[OC] UITableView 与 UItableViewCell 的使用

  UITableView 

//UIViewController里添加一个UITableView
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@end

//初始化,当然还有其他初始方法,不赘述了
_tableView = [[UITableView alloc] init];
//设置代理
_tableView.delegate = self;
_tableView.dataSource = self;
//界面添加
[self.view addSubview:_tableView];
//设置尺寸,当然也可以通过masonry之类的添加约束
[_tableView setFrame:self.view.frame];
//各种属性设置
//分割线(无分割线)
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//背景颜色
_tableView.backgroundColor = [UIColor WhiteColor];
//section数目
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

//每个section有几个单元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
///////////////////////////////////
//单元格具体实现 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //注意单元格的复用 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } return cell; } //单元格高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40; } //点击单元格触发的事件 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"你点了一个cell"); } ////////////////////////////////////////

//section头部的具体实现 - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return [[UIView alloc] init]; } //section头部高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 20; }
//section footer的具体实现 - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return [[UIView alloc] init]; } //section footer 高度 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 20; }

 

// 设置 cell 是否允许左滑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return true;
}
// 设置默认的左滑按钮的title
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"按钮钮钮";
}
// 点击左滑出现的按钮时触发
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击左滑出现的按钮时触发");
}
// 左滑结束时调用(只对默认的左滑按钮有效,自定义按钮时这个方法无效)
-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"左滑结束");
}

 

新建的UITableViewCell中加入这一句作为cell的初始化函数

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //在这里写入定制cell的内容
    }
    return self;
}

 

posted @ 2018-06-21 11:16  Oran  阅读(556)  评论(0编辑  收藏  举报