我需要一个分组的UITableView类似于“设置”应用中的Twitter帐户:
也就是说,一种形式或菜单,其中一些部分具有预先知道的一组静态单元,并且一些其他部分必须是动态的,并且允许以与添加帐户相同的方式插入附加行.我在.xib文件中管理UITableView.对于静态单元格,我已经分离出可以在视图控制器的cellForRowAtIndexPath:方法中加载的.xib文件.
我该怎么处理这种表?我没有找到任何示例代码.
cellForRowAtIndexPath:方法应该如何?可能需要保持静电细胞的强大性能?在表视图的相同.xib文件中直接设计每个静态单元格,并为它们设置插座是否更好? (虽然这不允许重用我的自定义单元格设计…)
我需要一些指导,以实现这一点,正确管理单元格和内存.提前致谢
解决方法
如果只返回单元格而不在cellForRowAtIndexPath中添加任何内容,那么动态原型单元格可以像静态原型单元一样工作,因此您可以使用动态原型将“静态”单元格和动态样式单元格(动态数量和内容可变) .
在下面的例子中,我在IB中创建了一个表视图控制器(具有分组的表视图),并将动态原型单元格的数量更改为3.我将第一个单元格的大小调整为80,并添加了一个UIImageView和两个标签.中间单元格是基本样式单元格,最后一个是具有单个居中标签的另一个定制单元格.我给他们各自的标识符.这是IB的样子:
然后在代码中,我做到了:
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five"];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 1)
return self.theData.count;
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
return 80;
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"TitleCell" forIndexPath:indexPath];
}else if (indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:@"DataCell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.row];
}else if (indexPath.section == 2) {
cell = [tableView dequeueReusableCellWithIdentifier:@"ButtonCell" forIndexPath:indexPath];
}
return cell;
}
正如你所看到的,对于“静态”单元格,我只是使用正确的标识符返回单元格,并且我正好在IB中设置了单元格.运行时的结果将看起来像您发布的三个部分的图像.