![](/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.