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.万能方式
    
   

猜你喜欢

转载自www.cnblogs.com/StevenHuSir/p/UITableView.html