1. 程式人生 > >iOS-UITableView詳解

iOS-UITableView詳解

1.簡單使用

//遵循代理
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
//初始化定義
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)style:UITableViewStylePlain];
_tableView.delegate = self; //代理
_tableView.dataSource = self;
[self.view addSubview:_tableView];
//分割線顏色 _tableView.separatorColor = [UIColor redColor]; //分割線型別 //_tableView.separatorStyle = UITableViewCellSeparatorStyleNone; //行高 //_tableView.rowHeight = 100; //背景顏色 _tableView.backgroundColor = [UIColor orangeColor]; //背景圖片 UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; imageView.image
= [UIImage imageNamed:@"2_4.jpg"]; _tableView.backgroundView = imageView; //設定每個區的行數 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _dataArray.count; } //設定區的個數 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 自定義cell HKSubTagCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 注意:如果cell從xib載入,一定要記得繫結標示符 // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // if (cell == nil) { // cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([HKSubTagCell class]) owner:nil options:nil][0]; // } // 獲取模型 HKSubTagItem *item = self.subTags[indexPath.row]; cell.item = item; //cell.textLabel.text = item.theme_name; return cell; } //設定行高 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80; } //Cell 點選事件處理 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }

2.處理Cell分割線佔據整個螢幕寬度

// 處理cell分割線佔據整個螢幕寬度
1.自定義分割線 
2.系統屬性(iOS8才支援) 
//清空tableView分割線內邊距 清空cell的約束邊緣
    self.tableView.separatorInset = UIEdgeInsetsZero;
//Cell內部
    self.layoutMargins = UIEdgeInsetsZero;
3.萬能方式