问题描述
我正在尝试将已分组的表格视图单元格设置为按钮,但是我似乎无法在 XCode 4.2 的界面构建器中或以编程方式找到在哪里执行此操作.
I'm trying to set a Table View Cell that has been grouped, as a button however I can't seem to find where to do it in the interface builder in XCode 4.2 or programmatically.
我尝试将表格视图单元格链接到 IBAction,但它只允许我创建或链接到 IBOutlet.
I've tried linking the table view cell to an IBAction, however it only lets me create or link to an IBOutlet.
作为临时修复,我在单元格中嵌入了一个矩形按钮,但按下时不会突出显示蓝色.
As a temporary fix I have embedded a rectangle button in the cell, but this doesn't highlight blue when pressed.
我在几个应用程序中看到过这项工作,一个例子是下面 Safari 应用程序中的清除历史记录和清除 Cookie 和数据按钮:
I've seen this work in several apps, an example is the Clear History and Clear Cookies and Data buttons in the Safari app below:
推荐答案
想象一下你的 IBAction 是这样的:
Imagine that your IBAction is like this:
-(IBAction)buttonPressed{
//Do my stuff
}
然后,您应该在您的委托中执行此操作:
Then, you should do this in your delegate:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//check if your cell is pressed
BOOL myCellIsPressed = ...
if(myCellIsPressed)
[self buttonPressed];
}
要检查您的单元格是否被按下,您有几种方法,例如,如果您知道表格中按钮的行:
To check if your cell was pressed, you have several ways, for example, if you know the row of your button in the table:
int myCellRow = 5;
if (myCellRow == IndexPath.row) //YES!
或者你可以在你的单元格中放置一个标签,然后检查是否是那个:
Or you can put a tag in your cell and then check if is that one:
#define myTag 5
创建单元格时:
UITableViewCell *myCell = ...
myCell.tag = myTag
在 didSelectRowAtIndexPath 中:
And in didSelectRowAtIndexPath:
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.tag == myTag) //YES!
这篇关于表格视图单元格作为按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!