目标:当用户选择单元格时,会向该单元格添加一个按钮.在我的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];
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎没有做任何事情.我是否重新绘制细胞校正?我需要以另一种方式添加吗?
BP.*_*BP. 23
尝试使用此代码块而不是上面提供的块:
UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadButton setTitle:@"Download" forState:UIControlStateNormal];
[downloadButton setFrame:CGRectMake(0, 0, 100, 35)];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;
Run Code Online (Sandbox Code Playgroud)
这应该显示按钮,但您仍然需要使用addTarget将某种选择器连接到它.(我不知道,如果在监听的accessoryButtonTappedForRowWithIndexPath代表将在这种情况下工作,首先尝试一下,看看它是否触发您按下按钮.)
将按钮指定为附件视图,而不是附件视图的子视图.
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = downloadButton;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31061 次 |
| 最近记录: |