我有一个tableView,其单元格在选择单元格时会扩展。我想知道如何向这些扩展单元格添加内容。
这就是扩展的方式:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.expandedCells containsObject:indexPath]) {
[self.expandedCells removeObject:indexPath];
}else{
isExpanded=YES;
[self.expandedCells addObject:indexPath];
}
[self.busTableView beginUpdates];
[self.busTableView endUpdates];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat kExpandedCellHeight =170;
CGFloat normalCellHeight = 94;
if ([self.expandedCells containsObject:indexPath]) {
return kExpandedCellHeight;
}else{
return normalCellHeight;
}
}
现在,当细胞扩展后,我该如何插入一些东西。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"Cell";
ListCell *cell =(ListCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:self options:nil];
cell = nibs[0];
}
if (isExpanded) { //tried setting BOOL var in didSelectRow , but it doesn't show the added button
UIButton *bButton = [UIButton buttonWithType:UIButtonTypeCustom];
[bButton setFrame:CGRectMake(100, 100, 40, 40)];
[bButton setTitle:@"Title" forState:UIControlStateNormal];
[cell addSubview:bButton];
}else{
cell.opName.text = [theArray objectAtIndex:indexPath.row];
}
return cell;
}
}
答案 0 :(得分:1)
在这里我要修改你的代码plz再次检查
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.busTableView beginUpdates];
if ([self.expandedCells containsObject:indexPath]) {
[self.expandedCells removeObject:indexPath];
}else{
[self.expandedCells addObject:indexPath];
}
[self.busTableView endUpdates];
[self.butTableView reloadData];
}
使用UITableViewCell类的ContentView属性
[cell.ContentView addSubView:bButton]
这是一个例子
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (isExpanded) {
UIButton *bButton = [UIButton buttonWithType:UIButtonTypeCustom];
[bButton setFrame:CGRectMake(100, 100, 40, 40)];
[bButton setTitle:@"Title" forState:UIControlStateNormal];
[cell.ContentView addSubView:bButton]
}