我有一个 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;
}
}
最佳答案
这里我要修改你的代码请再次检查
-(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]
}
关于ios - 如何向展开的 tableView 单元格添加按钮、标签等内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25281177/