【问题标题】:iPhone - Adding button to selected row in UITableViewiPhone - 将按钮添加到 UITableView 中的选定行
【发布时间】:2011-09-02 20:18:07
【问题描述】:

我正在使用 XCode 的基于导航的应用程序模板来创建一个以 UITableView 为中心的应用程序。


当用户在 UITableView 中选择一行时,我想在该选定单元格内显示一个按钮。我只想在选定的单元格中显示此按钮,而不是在任何其他单元格中。如果用户之后选择不同的单元格,情况也是如此。


我该怎么做呢?有可能吗?

【问题讨论】:

    标签: iphone uitableview button row cell


    【解决方案1】:

    子类 UITableViewCell 并向其添加按钮。

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
            button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
            [button setFrame:CGRectMake(320.0 - 90.0, 6.0, 80.0, 30.0)]; 
            [button setTitle:@"Done" forState:UIControlStateNormal];
            button.hidden = YES;
            [self.contentView addSubview:button];
        }
        return self;
    }
    

    然后像这样覆盖 setSelected:

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
        self.button.hidden = !selected;
    }
    

    【讨论】:

    • 谢谢。将尝试该解决方案。
    • 我试图让它工作,但无法让按钮显示。你的意思是这样的吗? - (void)setSelected:(BOOL)选定动画:(BOOL)animated { [super setSelected:selected animated:animated];按钮 = [[UIButton alloc] 初始化]; [按钮 setFrame:CGRectMake(20, 20, 20, 20)]; [按钮 setTitle:@"Done" forState:UIControlStateNormal]; self.button.hidden = !selected; [[self contentView]addSubview:button];我将 rootviewcontroller 更改为新的 UITableViewCell 子类。
    • 现在显示按钮。 :) 谢谢!一个小问题:当我单击按钮时,如何获取行的 indexPath?
    • 您可以从您的自定义表格视图单元格中添加一个委托回调并传递对该单元格的引用。然后监听器可以使用 [UITableView indexPathForCell:]。
    【解决方案2】:

    这应该可以使用以下内容:

    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (lastClickedCell != nil) {
            // need to remove button from contentView;
            NSArray *subviews = lastClickedCell.contentView.subviews;
            for (UIButton *button in subviews) {
                [button removeFromSuperview];
            }
        }
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        // this gives you a reference to the cell you wish to change;
        UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // you can change the type to whatever you want
        [cellButton setFrame:CGRectMake(x, y, w, h)]; // you will need to set the x,y,w,h values to what you want
        // if you want the button to do something, you will need the next line;
        [cellButton addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
        // now you will need to place the button in your cell;
        [cell.contentView addSubview:cellButton];
        [tableView reloadData];  // this updates the table view so it shows the button;
        lastClickedCell = cell;  // keeps track of the cell to remove the button later;
    }
    

    编辑:当您选择一个新单元格时,您当然需要从 contentView 中删除按钮,因此您需要一些逻辑。子类化可能是一个更简单的解决方案,但如果您不想子类化,这是您需要采取的方法。例如,您可能希望在标题中声明以下内容。

    UITableViewCell *lastClickedCell;
    

    然后你会想把它合并到上面(我会改变把它放进去);

    【讨论】:

      【解决方案3】:

      您是否查看 developer.apple.com 以获取 UITableViewController 和 UIButton 的文档?

      【讨论】:

        【解决方案4】:

        这是一个简单的解决方案!

        1. 在 viewDidLoad 函数之类的地方创建按钮(确保在 .h 文件中声明它,以便您可以从任何地方引用它)

        2. 在 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

        添加以下内容:

          if (yourButton)
                    [yourButton removeFromSuperview];
        
                [[tableView cellForRowAtIndexPath:indexPath] addSubview:yourButton];
                [yourButton setSelected:NO]; 
                [yourButton setHighlighted:NO];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-12
          • 1970-01-01
          • 2010-09-24
          相关资源
          最近更新 更多