添加按钮到UITableViewCell的附件视图

时间:2011-09-12 13:38:46

标签: ios uitableview

目标:当用户选择一个单元格时,会向该单元格添加一个按钮。在我的didSelectRowAtIndexPath函数中,我有以下内容:

UIButton *downloadButton = [[UIButton alloc] init];
downloadButton.titleLabel.text = @"Download";
[downloadButton setFrame:CGRectMake(40, 0, 100, 20)];
[[self.tableView cellForRowAtIndexPath:indexPath].accessoryView addSubview:downloadButton];
[[self.tableView cellForRowAtIndexPath:indexPath].accessoryView setNeedsLayout];

[downloadButton release];

不幸的是,这似乎没有做任何事情。我是否重新绘制细胞校正?我需要以另一种方式添加吗?

9 个答案:

答案 0 :(得分:23)

尝试使用此代码块而不是上面提供的块:

UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadButton setTitle:@"Download" forState:UIControlStateNormal];
[downloadButton setFrame:CGRectMake(0, 0, 100, 35)];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

这应显示按钮,但您仍需要使用addTarget将某种选择器连接到它。 (我不确定在这种情况下是否可以监听accessoryButtonTappedForRowWithIndexPath委托,先尝试一下,看看它是否会按下你的按钮。)

答案 1 :(得分:8)

我遇到了同样的问题。试图将accessoryView设置为具有图像的UIButton,导致它不会出现。

诀窍是调用[UIButton sizeToFit],以确保其框架设置正确。

答案 2 :(得分:7)

将按钮指定为附件视图,而不是附件视图的子视图。

UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = downloadButton;

答案 3 :(得分:1)

试试这个:

[[self.tableView cellForRowAtIndexPath:indexPath].contentView addSubview:downloadButton];

请记住在重复使用单元格时删除该按钮。

答案 4 :(得分:1)

以下是我的请求完整解决方案的示例:

在我的UITableViewCell子类中(我称之为RNWNewItemCell):

-(void)configureCell...
{
   // create the button
   self.btnSeekDuplicate = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
   // just make it yellow until we get our real image..
   self.btnSeekDuplicate.backgroundColor = [UIColor yellowColor];
   // add the handler for the button press
   [self.btnSeekDuplicate addTarget:self
                             action:@selector(seekDuplicateButtonWasPressed) 
                   forControlEvents:UIControlEventTouchUpInside];
   // make it visible in the table cell...
   [self setAccessoryView:self.btnSeekDuplicate];
}

- (void)seekDuplicateButtonWasPressed
{
    do something...
}

在我使用单元格的表格代码中......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   ...
   RNWNewItemCell *aNewItemCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierForNewItemCell forIndexPath:indexPath];
   [aNewItemCell configureCell...]
   ...
}

请注意,在设置表格单元格的accessoryView时,不会调用accessoryButtonTappedForRowWithIndexPath。可能是因为他们假设您正在使用响应事件的视图。

答案 5 :(得分:1)

let button = UIButton(type:.roundedRect)
button.setTitle("A", for: .normal)
button.sizeToFit()
cell.accessoryView = button

答案 6 :(得分:1)

Swift 4及更高版本:将按钮添加到UITableViewCell的附件视图

application.html.erb

添加选择器方法

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = Table.dequeueReusableCell(withIdentifier: "identifier", for: indexPath)

            let accessoryButton = UIButton(type: .custom)
            accessoryButton.addTarget(self, action: #selector(deleteCell(sender:)), for: .touchUpInside)
            accessoryButton.setImage("Add_image", for: .normal) 
            accessoryButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
            accessoryButton.contentMode = .scaleAspectFit

            cell.accessoryView = accessoryButton as UIView

            return cell
    }

答案 7 :(得分:0)

最好将要添加到单元格的任何视图添加到cell.contentView。 还要尝试检查accessoryView是否为零。

答案 8 :(得分:0)

使用它:

cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;