gpt4 book ai didi

ios - 将按钮添加到 UITableViewCell 的附件 View

转载 作者:IT王子 更新时间:2023-10-29 08:17:29 26 4
gpt4 key购买 nike

目标:当用户选择一个单元格时,将向该单元格添加一个按钮。在我的 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 委托(delegate)是否有效,请先尝试一下,看看它是否会在您按下按钮时触发。)

关于ios - 将按钮添加到 UITableViewCell 的附件 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7388812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com