【发布时间】:2012-02-03 11:32:18
【问题描述】:
我有一个表格视图,我在表格视图单元格中显示来自数据库的值。在这个 tableview cellforrowatindexpath 中,我创建了一个带有图像的按钮并为其设置选择器。因此,当我单击按钮时,我的选择器被调用,它会将我的图像更改为另一个图像。
但问题是它没有识别索引路径,即如果 tableview 中有 4 行并且如果我单击第一行按钮,它的图像应该改变但问题是执行第 4 行操作并且它的图像被改变,因为它没有得到正确的图像索引路径来改变。
这是我的 cellforrowatindexpath 代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 20, 20, 20);
mimageButton.tag = 1;
onButtonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:@selector(changeMapType::) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
这是我的 changemapType 代码
-(void)changeMapType:(NSIndexPath*)path1:(UIButton*)sender{
appDelegate.changeimagetype =!appDelegate.changeimagetype;
if(appDelegate.changeimagetype == YES)
{
onButtonView.image = [UIImage imageNamed:@"alarm_OF.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
appDelegate.changeimagetype = YES;
}
else
{
onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
appDelegate.changeimagetype = NO;
}
}
【问题讨论】:
-
mimageButton.tag 根据您的 cellidentfier 设置此标签值。并且不需要在选择器中发送任何东西作为参数
-
你做错事了
-
@Ron 能否请您详细说明一下
-
@Rani 你有多个部分吗?
-
@Narayana 我没有多个部分
标签: iphone objective-c