【ios】tableview总结

一.定义tableview

1.定义dataSource & delegate

在storyboard上选取tableview,在链接器上把dataSource & delegate都拉到viewcontroller的小圆点上

2.在头文件上继承<UITableViewDataSource,UITableViewDelegate>

3.声明tableview:把stroryboard上的tableview控件拉到.m中

二.使用

1.设置数据源:有两种方法:

第一种:固定的,现有的:可在viewdidload里面赋值

第二种:条件触发的,后有的:通过[self.tableviewname reloadData] 重新刷入数据

因为tableview会在界面一进入时就已经自动调用- (NSInteger)tableView(UITableView *)tableView numberOfRowsInSection:(NSInteger)section等函数

2.方法:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    _count = [_macArray count];
    NSLog(@"table view : %lu",(unsigned long)_count);
    return [_macArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellname = @"namecell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellname];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellname];
    }
    
    cell.textLabel.text = _macArray[indexPath.row];
    return cell;
}

 第一个方法,return 数组长度来确定cell数量

第二个方法生成tableviewcell

猜你喜欢

转载自jameskaron.iteye.com/blog/2346297