0

我正在开发一个应用程序并被困在一件事上。我试图自己解决我的问题,但无法解决。如果有人知道如何解决这个问题,请帮助我。

我创建了一个 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;
    }
}
4

2 回答 2

0

我认为您的问题是您没有使用正确的方法,我认为您需要实施

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath{ }

这里你有所选行的 indexPath 和 tableView 的实例,你需要做的就是实例化 TableViewCell 并检查你的复选标记。

于 2012-04-18T14:48:26.543 回答
0

在方法中声明复选框按钮cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

于 2013-05-29T13:27:13.417 回答