ios – 将按钮添加到UITableViewCell的附件视图

目标:当用户选择单元格时,将向该单元格添加一个按钮.在我的didSelectRowAtIndexPath函数中,我有以下内容:
UIButton *downloadButton = [[UIButton alloc] init];
downloadButton.titleLabel.text = @"Download";
[downloadButton setFrame:CGRectMake(40,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,35)];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

这应该显示按钮,但是您仍然需要使用addTarget连接某些选择器. (我不知道如果在这个例子中听的附件ButtonTappedForRowWithIndexPath委托将工作,请先尝试,看看它是否按下按钮触发.)

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...