![](/img/trans.png)
[英]UITableViewCell's Accessory View Button is not displaying in ipad portrait mode
[英]Add button to UITableViewCell's Accessory View
目标:当用户选择单元格时,会向该单元格添加一个按钮。 在我的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];
不幸的是,这似乎没有做任何事情。 我是否重新绘制细胞校正? 我需要以另一种方式添加吗?
尝试使用此代码块而不是上面提供的块:
UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadButton setTitle:@"Download" forState:UIControlStateNormal];
[downloadButton setFrame:CGRectMake(0, 0, 100, 35)];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;
这应该显示按钮,但您仍然需要使用addTarget将某种选择器连接到它。 (我不确定在这种情况下是否可以监听accessoryButtonTappedForRowWithIndexPath委托,先尝试一下,看看它是否会按下你的按钮。)
我有同样的问题。 试图将accessoryView设置为具有图像的UIButton导致它不出现。
诀窍是调用[UIButton sizeToFit]
,以确保其框架设置正确。
将按钮指定为附件视图,而不是附件视图的子视图。
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = downloadButton;
以下是我的请求完整解决方案的示例:
在我的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。 可能是因为他们假设您正在使用响应事件的视图。
let button = UIButton(type:.roundedRect)
button.setTitle("A", for: .normal)
button.sizeToFit()
cell.accessoryView = button
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
}
试试这个:
[[self.tableView cellForRowAtIndexPath:indexPath].contentView addSubview:downloadButton];
并且记得在重复使用单元格时删除该按钮。
迅速
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;
最好将要添加到单元格的任何视图添加到cell.contentView
。 还要尝试检查accessoryView是否为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.