我试图在选择时为 UITableViewCell
加载扩展单元格 XIB。我的普通单元格和扩展单元格的内容几乎相同,除了我要填充一些 View 的额外空间在展开的单元格中。现在为了区分普通单元格和展开的单元格,我将展开单元格内的 UILabel
颜色更改为不同的颜色。我可以在单击单元格时加载展开的单元格 XIB < strong>但是问题是每个单元格都被展开的单元格XIB 取代。唯一的区别是点击单元格的高度比其他单元格高。当我单击返回展开的单元格时,普通单元格 XIB
会再次加载。如何只为单击的单元格加载展开的单元格 XIB
?或者是否有更好的方法来实现这一目标?
下面是我如何扩展单元格。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.expandedCells containsObject:indexPath]) {
[self.expandedCells removeAllObjects];
isExpanded = NO;
[self.busTableView beginUpdates];
[self.busTableView reloadData];
[self.busTableView endUpdates];
}else{
isExpanded=YES;
if (self.expandedCells.count>0) {
[self.expandedCells removeAllObjects];
}
[self.expandedCells addObject:indexPath];
expCell.expContainer.hidden=YES;
//Some more code
[self.busTableView beginUpdates];
[self.busTableView reloadData];
[self.busTableView endUpdates];
}
}
cellForRow
方法如下所示。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (!isExpanded) { //BOOL variable set to NO in viewDidLoad initially
BusListCell *cell =(BusListCell*) [tableView dequeueReusableCellWithIdentifier:nil];
if (cell==nil) {
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"BusListCell" owner:self options:nil];
cell = nibs[0];
}
cell.busName.text = [[busArray objectAtIndex:indexPath.row]valueForKey:@"operatorName"];
return cell;
}else{
expCell = (ExpandedCell*)[tableView dequeueReusableCellWithIdentifier:nil];
if (expCell==nil) {
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil];
expCell = nibs[0];
}
expCell.bus_Name.text = [[busArray objectAtIndex:indexPath.row]valueForKey:@"operatorName"];
return expCell;
}
return nil;
}
最佳答案
而不是检查 cellForRowAtIndexPath:
if (!isExpanded) {}
你必须检查
if (![self.expandedCells containsObject:indexPath]){}
关于ios - 在 tableView iOS 中为扩展单元格加载自定义 XIB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26280049/