UITableView

来源:互联网 发布:js onclick没反应 编辑:程序博客网 时间:2024/05/22 18:55

UITableView 列表
1.拖拽一个Table View控件到现有的View上
2.连接
Table View连接到代码中,命名为 mainTableView
3.包含代理
UIViewController< UITableViewDataSource>
代理分为必选代理(@required)和可选代理(@optional)
按住command并点击代理名字查看即可
4.将代理设置为自己的
[self.mainTableView setDataSource:self];
5.写下代理函数
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}
//行内容
(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
6.实现上述代理函数
行内容的代理比较复杂,包含两种情况,默认UITableViewCell,不包含XIB,和自定义CELL,包含XIB
第一种:
static NSString *strIdentifier = @”myIdentifie”;
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
{ if(myCell == nil )
{
myCell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:strIdentifier];
}

//此处可以写cell的一些配置,内容,样式等
return myCell;}
第二种:
事先要先自定义一个UITableViewCell的子类,类名假设CRTableViewCell,并包含同名xib
第一步:导入头文件并在ViewDidLoad内加入自定义CELL xib的注册
[self.mytableview registerNib:[UINib nibWithNibName:@“CRTableViewCell” bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@“myIdentifier”]
在行内容函数里
static NSString *strIdentifier = @“myIdentifier”;
CRTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:strIdentifier];
//此处可以写cell的一些配置,内容,样式等
return cell;
若没有写Cell的配置等东西,则显示你在TableViewCell.xlb中设置的东西
若要更多功能,则增加代理

  [self.mainTableView setDelegate:self];
//行高
(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
}
//点击事件
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
}

0 0
原创粉丝点击