我在 Stack Overflow 上看到了一些类似的问题,但没有一个得到令人满意的答案。
我想要实现的是“设置”屏幕中的“Facebook 登录按钮”。
我想使用静态单元格来实现这一点。 但我很快发现我无法使用 Xcode 将“Action”连接到 UITableViewCell。
然后,我尝试使用内部带有 UIButton 的自定义 UITableViewCell 来实现相同的结果,但这导致了很多额外的样式问题,使其看起来与真正的 UITableViewCell 完全一样。
现在我设法使用以下解决方案使 UITableViewCell 的行为像一个按钮:
我将“登录按钮”UITableViewCell 的标识符更改为“loginButton”。我将以下代码添加到 TableView Controller 。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[selectedCell reuseIdentifier] isEqualToString:@"loginButton"]) {
NSLog(@"Clicked");
// Execute function to run code for Login button
}
}
我现在要执行一个函数,而不是执行一个 IBAction(如果我可以像 Xcode 中的一个按钮一样链接它就会是这种情况)。
这按预期工作。但是我提出这个问题的原因是:我想知道这是否是正确的方法。这个可以吗?这很糟糕吗?为什么这是好的或坏的?有更好的解决方案吗?谢谢!
最佳答案
这是一个合理的方式。
使用索引路径的类似解决方案是:
从 IB 为 Table View Cell 创建一个导出。
@property (strong, nonatomic) IBOutlet UITableViewCell *loginButtonCell;
然后实现 didSelectRowAtIndexPath: 方法。
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:[tableView indexPathForCell:self.loginButtonCell]])
{
// This will get called when you cell button is tapped
}
}
然后您可以重新订购,而无需修改您的代码
关于objective-c - 使用 UITableViewCell 作为按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330720/