===================================================
========================使用========================
参考:--developer:tableview Sample Code(TableViewSuite);
------------------------最基本------------
1.创建tableview:
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
注意:如果控制器是继承UITableViewController,则以上代码不用写,它本身自带tableview,并且相关配置也可好,直接重写相关代理方法即可。
2.实现相关代理方法 代码方式
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.xxArray count]; //default numOfSection is one
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"simpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [self.xxArray objectAtIndex:indexPath.row];
return cell;
}
xib方式
如果tableview是在xib中创建,则可以它里面add一个tableViewCell(创建了一个Prototype Cells),则cellForRowAtIndexPath可以简单点写():
注意cell的identify一定要对应上。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// Set up the cell.
cell.textLabel.text = [self.xxArray objectAtIndex:indexPath.row];
return cell;
}
------注意