使用UITableViewCell作为Button

Wer*_*ner 2 xcode objective-c button uitableview

我在Stack Overflow上阅读了一些类似的问题,但没有一个有令人满意的答案.

我想要实现的是设置屏幕上的"Facebook登录按钮".

Facebook设置屏幕

我想用静态单元实现这个目的.但我很快发现我无法使用Xcode将"Action"连接到UITableViewCell.

然后我尝试使用带有UIButton的Custom UITableViewCell来获得相同的结果,但是它导致了很多额外的样式问题,使它看起来像真正的UITableViewCell.

现在我设法使用以下解决方案使UITableViewCell像Button一样运行:

我将"登录按钮"UITableViewCell的标识符更改为"loginButton".我将以下代码添加到Table View Controller.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[selectedCell reuseIdentifier] isEqualToString:@"loginButton"]) {
        NSLog(@"Clicked");
        // Execute function to run code for Login button
    }
}
Run Code Online (Sandbox Code Playgroud)

而不是执行IBAction(如果我可以将它像Xcode中的按钮一样链接就是这种情况),我现在要执行一个Function.

这是按预期工作的.但我提出这个问题的原因是:我想知道这是否是正确的方法.这个可以吗?这不好吗?为什么这样好还是坏?有更好的解决方案吗?谢谢!

Cha*_*upt 8

这是一个合理的方式.

使用索引路径的类似解决方案是:

从IB创建表视图单元的插座.

@property (strong, nonatomic) IBOutlet UITableViewCell *loginButtonCell;
Run Code Online (Sandbox Code Playgroud)

然后实现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
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以重新订购,而无需修改代码