ios 将按钮添加到 UITableViewCell 的附件视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7388812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add button to UITableViewCell's Accessory View
提问by LDK
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?
不幸的是,这似乎没有任何作用。我是在重绘单元格校正吗?我需要以另一种方式添加它吗?
回答by BP.
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. (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.)
这应该会显示按钮,但您仍然需要使用 addTarget 将某种选择器连接到它。(我不确定在这种情况下侦听附件ButtonTappedForRowWithIndexPath 委托是否有效,请先尝试一下,看看它是否会在您按下按钮时触发。)
回答by Senseful
I had the same problem. Attempting to set the accessoryView to a UIButton which had an image caused it to not appear.
我有同样的问题。尝试将附件视图设置为具有图像的 UIButton 导致它不出现。
The trick was to call [UIButton sizeToFit]
, to ensure its frame is set properly.
诀窍是调用[UIButton sizeToFit]
,以确保其框架设置正确。
回答by progrmr
Assign the button as the accessory view rather than a subview of the accessory view.
将按钮指定为附件视图而不是附件视图的子视图。
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = downloadButton;
回答by Ecarrion
Try This:
尝试这个:
[[self.tableView cellForRowAtIndexPath:indexPath].contentView addSubview:downloadButton];
And remember to delete that button when the cell is being reused.
并记住在单元格被重用时删除该按钮。
回答by mdebeus
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. Probably because they assume you are using a view that responds to events.
请注意,当您设置表格单元格的附件视图时,不会调用附件按钮TappedForRowWithIndexPath。可能是因为他们假设您使用的是响应事件的视图。
回答by Vyacheslav
let button = UIButton(type:.roundedRect)
button.setTitle("A", for: .normal)
button.sizeToFit()
cell.accessoryView = button
回答by Furkan Vijapura
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
}
回答by user1107173
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
}
回答by user1107173
use this :
用这个 :
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
回答by Rick
Its always best to add any views that you are adding to a cell to cell.contentView
.
Also try to check if the accessoryView is nil.
最好将要添加到单元格的任何视图添加到cell.contentView
. 还要尝试检查附件视图是否为零。