【问题标题】:How to add a content view on UITAbleViewCell when a row is selected?选择行时如何在 UITableViewCell 上添加内容视图?
【发布时间】:2012-10-18 11:08:58
【问题描述】:

我正在尝试在 UITableView 中选择一行时添加一个按钮,并在第二次点击该行时将其删除,我不想要自定义 UITableViewCell

任何建议或示例代码将不胜感激。

我尝试过的代码: 在 cellForRowAtIndexPath 方法中

if(cell.selected == YES){

                UITextField* numOfBottles =[[UITextField alloc] initWithFrame:CGRectMake(240,9.0f,50, 25)];
                numOfBottles.tag = indexPath.row;

                [numOfBottles setBorderStyle:UITextBorderStyleNone];
                [numOfBottles setBackgroundColor:[UIColor clearColor]];
                [numOfBottles setTextColor:[UIColor whiteColor]];
                [numOfBottles setTextAlignment:UITextAlignmentLeft];
                [numOfBottles setBackground:[UIImage imageNamed:@"blue_dropdown_normal.png"]];
                [numOfBottles setFont:[UIFont systemFontOfSize:16.0f]];
                [numOfBottles setDelegate:self];

                NSString* quantity = [[NSString alloc] initWithString:[subtotalObj.qtyArray objectAtIndex:(indexPath.row - 1)]];

                [numOfBottles setText:quantity];
                [numOfBottles setTextAlignment:UITextAlignmentCenter];
                [numOfBottles setBackgroundColor:[UIColor whiteColor]];
                numOfBottles.keyboardType = UIKeyboardTypeDefault;
               // numOfBottles.tag = indexPath.row;
                [cell.contentView addSubview:numOfBottles];
                [numOfBottles release];
            }

didSelectRowAtIndexPath 方法中

[tableView reloadData];

但按钮(带有背景图像的文本字段)仍然没有呈现。

【问题讨论】:

    标签: objective-c ios ios5 uitableview


    【解决方案1】:

    我认为最好的方法是使用 UITableViewCell 子类。 但是您可以使用我在 another question 上的回答来重新渲染您的 TableView 并在您的单元格中放置一个按钮。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        if(indexPath.row == selectedRow){ // if your criteria where met
          //add your button
        }
    
         return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        //do your select things here
      selectedRow = indexPath.row; //where selectedRow is a class variable
      [self.tableView reloadData]; // rerenders the tableview
    
    }
    

    【讨论】:

    • 我建议你给我们一个代码 sn-p 你试过的。
    • 我添加了一个变量,这是我认为最快的方式。 cell.selected 仅适用于 UITableViewCell 子类
    【解决方案2】:

    UITableView 代表,- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 实现它,您将在其中获取用户点击的行,使用cellForRowAtIndexPath: 获取当前cell,您需要使用section 和@ 传递NSIndexPath 987654327@。现在检查cell.contentView subviews 是否有您想要的UIButton?如果是,则执行removeFromSuperView,否则执行addSubView。如果你cell已经填写了UIButtons,你可以给unique标签来区分它们。

    【讨论】:

      【解决方案3】:

      使用这些委托

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        //Create reference of button with tag 999
        cell.contentView addSubView:yourButtonReference]
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
      

      }

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
          UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
          UIButton *btnAdded = (UIButton *)[cell viewWithTag:999];
          if(btnAdded){
            [btnAdded removeFromSuperView];
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多