您的位置:首页 > 移动开发 > IOS开发

ios tableView那些事 (十五)用故事模式自定义cell

2013-09-09 14:45 711 查看
如果想更灵活的添加一些数据我们常常自定义一个cell

先创建个工程 勾选sb 和arc  



把默认的里面的viewcontr 删除,拖拽个tableviecontr



创建一个TableViewControllerTest  一个类,这时候我们要把 拖拽的视图和这个新建的类关联上



接下来我们新建一个继承 uitableviewcell 的类  celltest



下面我们吧这个类跟tableview关联上

点击视图上面的cell 并把新建的cell关联上!  如图在custom class 下面选择CellTest



把cell 的格式选择Custom     把Identifier 改成你cell 的类名



然后让我们托几个label吧



在tableview上在加几行代码就搞定了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier =@"cell";
    
   //CellTest *cell =  (CellTest *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    CellTest *cell = (CellTest *)[tableViewdequeueReusableCellWithIdentifier:@"CellTest"];
    if (cell == nil) {
        cell = [[CellTestalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier];
    }
    cell.label_one.text =@"one";
    cell.label_second.text =@"second";
    cell.labelthree.text =@"three";
    
    
    return cell;
}

   ///////////////////////////////////14 年5.10///////////////////////////////////////////////////////
 自定义一个tableview 后不显示数据,我排查了连接 委托是否有问题后,发现一切正常,后来看了下代码,发现我copy 的标识符GoodsCarTableViewCell 后面有了一个

 空格,我把空格除去果断出现想要的数据,真是马虎呀。
 GoodsCarTableViewCell *cell = (GoodsCarTableViewCell*) [tableView    dequeueReusableCellWithIdentifier:@"GoodsCarTableViewCell
 此处有空格 "];  错误 

正确
 GoodsCarTableViewCell *cell = (GoodsCarTableViewCell*) [tableView    dequeueReusableCellWithIdentifier:@"GoodsCarTableViewCell"];
 后面无空格正确。
   ///////////////////////////////////14 年5.10///////////////////////////////////////////////////////

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uitableview
相关文章推荐