将按钮添加到UITableViewCell的附加视图

20

目的:当用户选择一个单元格时,向该单元格添加一个按钮。在我的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];

很不幸,那似乎没有起作用。我是否需要重新绘制单元格修正?还需要以其他方式添加它吗?

10个回答

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 委托会起作用,请先尝试一下,看看它是否会在按下按钮时触发。)


5
实际上,我尝试使用accessoryButtonTappedForRowWithIndexPath方法,但显然它不会在自定义辅助视图按钮点击时触发。 - BP.
4
令人惊讶的是,似乎需要使用UIButtonTypeRoundedRect。我尝试了UIButtonTypeCustom但它没有起作用。+1 - nickjwallin

14

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

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


8
将按钮指定为附件视图而不是附件视图的子视图。
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = downloadButton;

4

Swift 4及以上版本:在UITableViewCell的附加视图中添加按钮

    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
    }

添加选择器方法

    func deleteCell(sender: AnyObject)
    {
        let pointInTable: CGPoint = sender.convert(sender.bounds.origin, to: self.Table)
        let cellIndexPath = self.Table.indexPathForRow(at: pointInTable)
        let point = cellIndexPath!.row

    }

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

1

试一下:

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

当单元格被重用时,请记得删除该按钮。


你说你在didSelectRowAtIndexPath方法里,但是你正在使用self.tableView,为什么不尝试使用didSelectRowAtIndexPath方法提供给你的UITableView呢? - Ecarrion

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。可能是因为它们假定您正在使用响应事件的视图。

0
使用这个:
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

0

Swift

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    let accessoryButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as! UIButton
    cell.accessoryView = accessoryButton

    return cell
}

0

最好将任何要添加到单元格的视图添加到cell.contentView中。同时,尽量检查accessoryView是否为空。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接