当前位置: 移动技术网 > 移动技术>移动开发>IOS > 详解ios中自定义cell,自定义UITableViewCell

详解ios中自定义cell,自定义UITableViewCell

2019年07月24日  | 移动技术网移动技术  | 我要评论

通过继承uitableviewcell来自定义cell

1、创建一个空的项目、命名:

2、创建一个uitableviewcontroller 并且同时创建xib:

3、设置appdelegate.m中window的根控制器为刚刚创建的tableviewcontroller:

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions 
{ 
  self.window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; 
  tableviewcontroller *tableviewcontroller = [[[tableviewcontroller alloc] init] autorelease]; //自动释放 
  //设置根控制器 
  self.window.rootviewcontroller = tableviewcontroller; 
  [self.window makekeyandvisible]; 
  return yes; 
} 

4、创建自定义的uitableviewcell:

5、创建自定义cell的xib 拖放需要的控件
选择user interface。

创建空的xib。

拖入cell控件。

完成自定义的cell控件。

设置cell控件的identfier。

绑定cell类并且将控件的输出口关联到tableviewcell.h文件中。

6、对tableviewcontroller类编码,在委托方法中设置自定义的cell:

#import "tableviewcontroller.h" 
#import "tableviewcell.h" 
 
@interface tableviewcontroller (){ 
  nsmutablearray *tabledata; //表格数据 
} 
 
@end 
 
@implementation tableviewcontroller 
 
- (id)initwithstyle:(uitableviewstyle)style 
{ 
  self = [super initwithstyle:style]; 
  if (self) { 
    // custom initialization 
  } 
  return self; 
} 
 
- (void)viewdidload 
{ 
  [super viewdidload]; 
  //初始化表格数据 
  tabledata = [[nsmutablearray alloc] init]; 
  for (int i = 0; i< 10; i++) { 
    [tabledata addobject:[nsstring stringwithformat:@"mycelldemon%i",i]]; 
  } 
 
  //设置row的高度为自定义cell的高度 
  self.tableview.rowheight = 90; 
  
} 
 
- (void)didreceivememorywarning 
{ 
  [super didreceivememorywarning]; 
 } 
 
#pragma mark - table view data source 
 
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview 
{ 
#warning potentially incomplete method implementation. 
   return 1; 
} 
 
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section 
{ 
#warning incomplete method implementation. 
   return [tabledata count]; 
} 
 
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath 
{ 
  //指定cellidentifier为自定义的cell 
  static nsstring *cellidentifier = @"tableviewcell"; 
  //自定义cell类 
  tableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; 
  if (cell == nil) { 
    //通过xib的名称加载自定义的cell 
    cell = [[[nsbundle mainbundle] loadnibnamed:@"tableviewcell" owner:self options:nil] lastobject]; 
  } 
   
  //添加测试数据 
  cell.titlelabel.text = [tabledata objectatindex:indexpath.row]; 
  cell.content.text = @"这是一些测试数据"; 
  //测试图片 
  cell.iamge.image = [uiimage imagenamed:@"testimage.jpg"]; 
   return cell; 
} 
 
#pragma mark - table view delegate 
 
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath 
{ 
 
} 
 
@end 

最终效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网