使用的UITableViewCell的按钮 [英] Use UITableViewCell as Button
问题描述
我读到这个堆栈溢出一些类似的问题,但他们都没有一个令人满意的答案。
I read some similar questions about this on Stack Overflow, but none of them had a satisfying answer.
我想实现一个Facebook的登录按钮,从设置屏幕。
What I am trying to achieve is a "Facebook Sign In Button" from the Settings screen.
我要实现这个使用静态细胞。
但我很快发现,利用X code我无法连接一个行动来的UITableViewCell。
I want to achieve this using Static Cells. But I soon discovered that I could not connect a "Action" to the UITableViewCell using Xcode.
我又试图用一个自定义的UITableViewCell里面一个UIButton来达到同样的效果,但它造成了很多额外的麻烦造型让它看起来完全像一个真正的UITableViewCell。
I then tried to achieve the same result using a Custom UITableViewCell with a UIButton inside, but it resulted it a lot of extra styling trouble to make it look exactly like a real UITableViewCell.
现在我设法使的UITableViewCell使用以下解决方案来像一个按钮:
Now I managed to make the UITableViewCell to behave like a Button using the following solution:
我改变了登录按钮的UITableViewCell的标识符为loginButton。我增加了以下code将表视图控制器。
I changed the Identifier of the "Login Button" UITableViewCell to "loginButton". And I added the following code to the 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
}
}
而不是执行IBAction为(这将是这种情况,如果我可以链接它像X code进行按钮),现在我要执行功能。
Instead of executing a IBAction (which would have been the case if I could just link it like a button in Xcode) I am now going to execute a Function.
这是工作像预期。但是,我创造了这个问题的原因是:我想知道如果这是正确的道路要走。这个可以吗?这是坏?这是为什么好还是不好呢?有没有更好的解决方案吗?谢谢!
This is working like expected. But the reason I created this question is: I want to know if this is the right way to go. Is this ok? Is this bad? Why is this ok or bad? Is there a better solution? Thanks!
推荐答案
这就是一个合理的路要走。
Thats a reasonable way to go.
使用indexpaths类似的解决方案是:
A similar solution using indexpaths would be:
创建从IB的表视图单元格的插座上。
Create an outlet for the Table View Cell from IB.
@property (strong, nonatomic) IBOutlet UITableViewCell *loginButtonCell;
然后实现didSelectRowAtIndexPath方法:方法
Then implement the didSelectRowAtIndexPath: method.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:[tableView indexPathForCell:self.loginButtonCell]])
{
// This will get called when you cell button is tapped
}
}
您可以重新秩序,而无需修改code
You can then re-order and without having to modify your code
这篇关于使用的UITableViewCell的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!