ios - 如何向展开的 tableView 单元格添加按钮、标签等内容?

标签 ios objective-c uitableview

我有一个 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/

相关文章:

objective-c - 在 Objective-C 中打印 C++ 数组 "NSLog"

ios - UITableView 不会显示删除按钮

ios - 我的 UITableView 中没有显示任何数据

UITableView 和 ASIHTTPRequest

ios - 最后一个单元格在 Tableview 中未正确显示

ios - Swift - 在 vi​​ewDidLoad() 之外声明变量的替代方法

Android - 我可以在给定的标签上打开 Instagram 应用程序吗?

ios - 为什么字典中的 "values"属性不是一个简单的数组,而是一些奇怪的东西?

ios - 在默认选中一个单元格的单元格之间切换复选标记

iphone - 如何在 iOS 上使用 Facebook API Graph 增加签到地点的数量