简体   繁体   English

如何使表格视图单元格作为按钮工作?

[英]How do I make a table view cell work as a button?

我正在尝试创建一个应用程序,用户将按下一个单元格,单元格将作为一个按钮,现在我如何使单元格功能作为一个按钮并执行操作或如何知道正在按哪个单元格?

Please read the document but basically when you set the delegate of the tableviewcontroller it calls this method when clicked: 请阅读该文档,但基本上当您设置tableviewcontroller的委托时,它会在单击时调用此方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

The indexpath holds the section and the row. 索引路径包含节和行。 If you are using only one section then indexPath.row will return the index of the cell that was clicked. 如果您只使用一个部分,则indexPath.row将返回单击的单元格的索引。

You implement the UITableViewDelegate method tableView:didSelectRowAtIndexPath: . 您实现了UITableViewDelegate方法tableView:didSelectRowAtIndexPath: .

That method will be called whenever the user taps any of the cells. 只要用户点击任何单元格,就会调用该方法。 If the passed indexPath is the index path of your button cell, then you do whatever should happen. 如果传递的indexPath是按钮单元格的索引路径,那么您可以执行任何操作。

Read Apple's documentation on UITableViewDelegate 阅读有关UITableViewDelegate的 Apple文档
And you will find tableView:didDeselectRowAtIndexPath: 你会发现tableView:didDeselectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

this method gets called when the user taps a cell. 当用户点击一个单元格时,会调用此方法。 indexPath.section and indexPath.row are your cell section and row indexs. indexPath.sectionindexPath.row是您的单元格部分和行indexPath.row http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html

If I understood your question correctly, you simply want to run some code when a user touches a cell. 如果我正确理解了您的问题,您只需要在用户触摸单元格时运行一些代码。

The UITableViewDelegate has a method called tableView:didSelectRowAtIndexPath: ( UITableViewDelegate tableView:didSelectRowAtIndexPath: ) UITableViewDelegate有一个名为tableView:didSelectRowAtIndexPath:的方法tableView:didSelectRowAtIndexPath:UITableViewDelegate tableView:didSelectRowAtIndexPath:

Just implement that and you can do what you want in there. 只要实现它,你就可以在那里做你想做的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM