1
我有我在我的CustomCell创建一个按钮。如何在UITableViewCell中使用按钮进行操作?
- (IBAction)onoffBtn:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
if (button.selected)
{
// Do action 1
}
else
{
// Do action 2
}
}
当我运行它时,按钮显示在我创建TableViewController UITableView的单元格中。我可以在我的牢房内按下onoffBtn,但它不能采取行动。我该如何做,以便我的按钮可以执行操作?
这是我的我的TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = _customCell;
_customCell = nil;
}
// Show my cell
return cell;
}
和其它问题内的cellForRowAtIndexPath,在那里我应该写在做的过程吗?在我的TableViewController.m或我的CustomCell.m?如果你不介意,请给我提供代码和解释,即时通讯新的在这里,仍然在学习。我想了解你编写的每一行代码。谢谢。
更新:
这是我的cellForRowAtIndexPath里面TableViewController.m至今:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = _customCell;
_customCell = nil;
}
button.tag = indexPath.row;
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.timerLabel.text = [timeArray objectAtIndex:indexPath.row];
time = [[secondArray objectAtIndex:indexPath.row] integerValue];
NSLog(@"Title %@, time %@, second %@, time %i, tag %d", titleArray, timeArray, secondArray, time, button.tag);
return cell;
}
,这是日志说:
2012-08-04 17:38:36.257 Table[1269:f803] Title (
"Test 1",
"Test 2"
), time (
"10 secs",
"5 secs"
), second (
10,
5
), time 10, tag 0 // tag 0 for cell 1
2012-08-04 17:38:36.261 Table[1269:f803] Title (
"Test 1",
"Test 2"
), time (
"10 secs",
"5 secs"
), second (
10,
5
), time 5, tag 0 // tag should be 1 for cell 2 but it said 0. How to fix it?
谢谢你的快速回答,即时通讯初学者。我能问一些问题吗?我已经在我的CustomCell中放置了一个按钮,我是否应该再次使用您的代码添加一个按钮?非常感谢你 – Piyo 2012-08-04 08:02:40
,你可以删除按钮,添加此代码,这将根据框架设置动态添加一个按钮,所有的细胞,或者如果你的细胞是恒定的,你有你自己通过厦门国际银行,你可以简单地连接同一添加按钮IBAction为在厦门国际银行文件中的所有按钮 – superGokuN 2012-08-04 08:05:46
先生,我已经删除,但我可以用我的代码连接我的按钮,我把button.tag = indexPath.row;在我的cellForRowAtIndexPath和NSLog中。但是当我用我的tableview中的2个单元格运行它时,NSLog显示的两个标签号都是0,而不是0和1.我忘记了什么吗?请教,如何让我的按钮可以设置正确的标签? – Piyo 2012-08-04 09:53:44