ios控件 UITableViewCell的自定义

很多时候都会用到自定义的uitableviewcell来实现自己的需求。

下面以自定义CustomCell为例子:

MyCell.h文件:

1
2
3
4
5
6
7
@interface  MyCell : UITableViewCell
{
     UITextView *myTextView;
}
- ( IBAction )btnAction:( id )sender;
@property  (retain, nonatomic ) IBOutletUITextView *myTextView;
@end

MyCell.m文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import "MyCell.h"
@implementation  MyCell
@synthesize  myTextView;
 
- ( id )initWithStyle:(UITableViewCellStyle)style reuseIdentifier:( NSString  *)reuseIdentifier
{
     self  = [ super  initWithStyle:style reuseIdentifier:reuseIdentifier];
     if  ( self )
     {
     }
     return  self ;
}
 
- ( void )setSelected:( BOOL )selected animated:( BOOL )animated
 
{  [ super  setSelected:selected animated:animated];}
 
- ( IBAction )btnAction:( id )sender {}
1
2
3
4
5
- (UITableViewCell *)tableView:(UITableView *)tableView  //nib设置了重用标识符,则tableview会使用重用机制
          cellForRowAtIndexPath:( NSIndexPath  *)indexPath
{
     static  NSString  *cellid=@ "CellReuseID" ;
     MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellid];(寻找标识符为cellid并且没被用到的cell用于重用)
1
if (cell== nil ) <br>  { <br>    cell = [[[ NSBundle  mainBundle] loadNibNamed:@ "MyCell"  owner: self  options: nil ] lastObjects]; <br> //如果此nib没有设置标识符,则当其移出屏幕时会自动释放(dealloc),可以用cell = [MyCell alloc] init];使其不自动释放<br>  }<br>   NSUInteger row = [indexPath row]; <br>  [cell.myTextView setText:@"123456"]; <br>  cell.myTextView.editable = NO; <br>  return cell;<br> }
总结:很多客户端都有漂亮的cell外观,都是通过自定义cell来实现的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值