我正在开发一个应用程序,并坚持一件事。我试图自己解决我的问题,但无法弄清楚。如果有人知道如何解决这个问题,请帮助我。TableView行与iPhone中的复选框
我在每一行都创建了一个带有复选框的UITableView。当点击任何复选框时,我只想选中该复选框,但当前仅选中最后一个复选框。我已经在下面提供了我的当前代码,请让我知道是否看到任何应该更改的内容。
谢谢。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [selectPlayer objectAtIndex:indexPath.row];
checkboxButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 8, 25, 25)];
checkboxButton.tag = indexPath.row;
[checkboxButton setTitle:nil forState:UIControlStateNormal];
[checkboxButton addTarget:self action:@selector(checkboxButton:)forControlEvents:UIControlEventTouchUpInside];
[checkboxButton addSubview:checkImage];
cell.accessoryView = checkboxButton;
return cell;
}
-(void)checkboxButton:(id)sender {
NSIndexPath *indexPath = [selectTableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
[checkImage setImage:[UIImage imageNamed:@"checkbox-checked.png"]];
NSLog(@"button pressed at section %d and %d row", indexPath.section, indexPath.row);
break;
case 1:
//[checkImage setImage:[UIImage imageNamed:@"checkbox-checked.png"]];
NSLog(@"button pressed at section %d and %d row", indexPath.section, indexPath.row);
break;
default:
break;
}
break;
default:
break;
}
}
如果我理解你的权利,你必须在每一个小区的按钮实现代码如下。一旦你点击一个特定单元格的按钮,只有这个图像应该改变。 您是否在NSLog输出中获得正确的节和行值? – user944351 2012-04-17 14:05:35
这将导致问题,因为每当单元格向下滚动向下滚动再次,tableview将不会创建新的单元格,它会重用已分配的单元格,所以这将是问题,因此存储复选框的值在某些数据结构,如数组或字典,当点击复选框时,在数组结构中设置数值当从数据结构中获取值 – 2012-04-17 18:37:28
@ user944351否我没有得到正确的部分并且它总是显示第0节第0行这就是为什么我没有得到 – vishiphone 2012-04-18 03:39:17