UITableView

作者: 文艺小年青 | 来源:发表于2017-08-12 17:54 被阅读0次
#import "ViewController.h"

@interface ViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic,retain)NSArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = @[@"文强",@"小明",@"小强",@"额尔敦"];
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor redColor];
    //设置行高。
    tableView.rowHeight = 200.0f;
    //分割线的样式
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //分割线的颜色
    tableView.separatorColor = [UIColor cyanColor];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 300)];
    headerView.backgroundColor = [UIColor greenColor];
    tableView.tableHeaderView = headerView;

}
//设置有几个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 10;
}

//设置某一section中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.dataArray.count;
}
//设置某一位置的cell,屏幕向上滑,每次出现一cell,这个方法走一次
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *name = self.dataArray[indexPath.row];
    
    if ([@"额尔敦" isEqualToString : name ]) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"eerdun"];
        if (nil == cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"eerdun"];
        }
        return cell;
    }

    //当第一页的时候,重用池cell为空,
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //这里判断,如果重用池为空, 走判断里边的方法,创建一个cell,
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = name;
    
    //下边的这句不可以写到判断里边,一旦写到判断里边,当重用池有东西以后,就不会再走这个方法,
    //cell.textLabel.text = [NSString stringWithFormat:@"当前的行数:%ld",indexPath.row];
    return cell;
}
//当点击某一个cell的时候回触发该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //输出第几个分组第几行的内容
    NSLog(@"section :%ld,row:%ld,content:%@",indexPath.section,indexPath.row,self.dataArray[indexPath.row]);
    //取消选中的状态,点一下就么有了
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end

相关文章

网友评论

    本文标题:UITableView

    本文链接:https://www.haomeiwen.com/subject/ltujrxtx.html