自定义UITableViewCell

来源:互联网 发布:cordova.js开发文档 编辑:程序博客网 时间:2024/06/05 02:42

http://xiaohui3837843.blog.163.com/blog/static/54388740201151635857655/

。在XCode中选择新建->User Interface->Empty XIB。(指定一个有意义的名字)

2。打开空的xib文件,将UITableViewCell拖到xib窗口中。

3。添加样式和其他控件到里面去,(UITextField & UITextView 不适用于表格视图单元)

4。打开属性检查器,设置重用标识符号Identifier,如:BaseTableCell

 自定义UITableViewCell - 木木 - 木木的博客
//选中Title lable,设置它的tag属性值为101,之后便可以利用自定义视图中Title lable的tag进行对cell的子视图的操作:

#define TEXTLABEL ((UILabel *)[cell viewWithTag:101])


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//设置重用

      UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"BaseTableCell"];

      if(!cell)

{

cell = [[[NSBundle mainBundleloadNibNamed:@"BaseTableCell" owner:selfoptions:nillastObject];

}

        //设置cell的背景

[cell setSelectedBackgroundView:[[UIImageView allocinitWithImage:[UIImage imageNamed:@"cell_bg.png"]]];

[TEXTLABEL setText:[[UIFont familyNamesobjectAtIndex:indexPath.row]];

return cell;

}


//可以在以上代理中设置单元格的高度 tableView.rowHeight = 100;

//也可以在专门设置高度的代理方法中设置表单元格高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 100;

}


//自定义单元格背景颜色

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

[cell setBackgroundColor:[UIColor redColor]];  //设置背景颜色

[cell setSelectedBackgroundView:[[UIImageView allocinitWithImage:[UIImage imageNamed:@"cell_bg.png"]]]; //设置选中后的背景,同前一个方法。

}


0 0