简体   繁体   English

添加按钮到UITableViewCell的附件视图

[英]Add button to UITableViewCell's Accessory View

Goal: when a user selects a cell, a button is added to that cell. 目标:当用户选择单元格时,会向该单元格添加一个按钮。 Within my didSelectRowAtIndexPath function I have the following: 在我的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];

Unfortunately that doesn't seem to do anything. 不幸的是,这似乎没有做任何事情。 Am I redrawing the cell correction? 我是否重新绘制细胞校正? Do I need to add it another way? 我需要以另一种方式添加吗?

Try this block of code instead of the block you provided above: 尝试使用此代码块而不是上面提供的块:

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

This should display the button, but you will still need to hook up some kind of selector to it using addTarget. 这应该显示按钮,但您仍然需要使用addTarget将某种选择器连接到它。 (I am not sure if listening in for the accessoryButtonTappedForRowWithIndexPath delegate will work in this case, try that first and see if it fires on your button press.) (我不确定在这种情况下是否可以监听accessoryButtonTappedForRowWithIndexPath委托,先尝试一下,看看它是否会按下你的按钮。)

I had the same problem. 我有同样的问题。 Attempting to set the accessoryView to a UIButton which had an image caused it to not appear. 试图将accessoryView设置为具有图像的UIButton导致它不出现。

The trick was to call [UIButton sizeToFit] , to ensure its frame is set properly. 诀窍是调用[UIButton sizeToFit] ,以确保其框架设置正确。

Assign the button as the accessory view rather than a subview of the accessory view. 将按钮指定为附件视图,而不是附件视图的子视图。

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

Here is my example for a the full solution to your request: 以下是我的请求完整解决方案的示例:

In my UITableViewCell subclass (I call it RNWNewItemCell): 在我的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...
}

In my Table Code that uses the cell... 在我使用单元格的表格代码中......

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

Note that accessoryButtonTappedForRowWithIndexPath is NOT called when you set the table cell's accessoryView. 请注意,在设置表格单元格的accessoryView时,不会调用accessoryButtonTappedForRowWithIndexPath。 Probably because they assume you are using a view that responds to events. 可能是因为他们假设您正在使用响应事件的视图。

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

Swift 4 and above: Add button to UITableViewCell's Accessory View 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
    }

Add Selector Method 添加选择器方法

    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

    }

Try This: 试试这个:

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

And remember to delete that button when the cell is being reused. 并且记得在重复使用单元格时删除该按钮。

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
}

用这个 :

cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

Its always best to add any views that you are adding to a cell to cell.contentView . 最好将要添加到单元格的任何视图添加到cell.contentView Also try to check if the accessoryView is nil. 还要尝试检查accessoryView是否为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM