【发布时间】:2012-02-14 19:05:51
【问题描述】:
我目前正在开发一个包含 UITableView 的应用程序。 UITableView 包含自定义单元格。在单元格内,我提供了一个 UIbutton,它将用于多行选择。在 tableView 目前我有 30 行并且工作正常但是,当我单击第一行中的按钮时,第 6、11、16、21、26 行中的按钮也会被单击,如果单击之后的第二行下一行被点击(即第 7 行、第 12 行等等...)。
代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//
// Create the cell.
//
cell =
[[[UITableViewCell alloc]
initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier]
autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
indicatorImage = [UIImage imageNamed:@"NotSelected.png"];
indicatorHighImage = [UIImage imageNamed:@"IsSelected.png"];
selectbtn = [UIButton buttonWithType:UIButtonTypeCustom];
selectbtn.frame = CGRectMake(30,122,20,20);
[selectbtn setImage:indicatorImage forState:UIControlStateNormal];
selectbtn.tag = rowCount++;
[cell addSubview:selectbtn];
[selectbtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
-(IBAction)buttonClicked:(UIButton *)button
{
if([button isSelected])
{
[button setImage:indicatorImage forState:UIControlStateNormal];
[button setSelected:NO];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Custom Button Pressed"
message:[NSString stringWithFormat: @"You pressed the custom button on cell #%i", button.tag]//pathToCell.row + 1]
delegate:self cancelButtonTitle:@"Great"
otherButtonTitles:nil];
[alertView show];
[alertView release];
[button setImage:indicatorHighImage forState:UIControlStateNormal];
[button setSelected:YES];
}
return;
}
【问题讨论】:
-
您在表格视图单元格中以文本形式显示的数据类型
-
我在单元格内显示了一个按钮和一些文本标签。目前我已经对数据进行了硬编码...
-
我认为您正在使用数组在标签上显示文本。如果是数组,那么数组中包含的是 NSString 还是 NSDictionaries?
-
我没有使用数组。我在硬编码的所有行中显示相同的文本
-
然后你只需要在你被选中的按钮上选择或取消选择图像。够了吗
标签: iphone objective-c ios ipad xcode4.2