简体   繁体   English

iPhone中带有CheckBox的TableView行

[英]TableView rows With CheckBox in iPhone

I'm developing an application and am stuck on one thing. 我正在开发一个应用程序,并且停留在一件事情上。 I've tried to resolve my problem on my own, but couldn't figure it out. 我试图独自解决问题,但无法解决。 If some one knows how to solve this, please help me out. 如果有人知道如何解决此问题,请帮助我。

I created one UITableView with a checkbox in every row. 我创建了一个UITableView,每一行都有一个复选框。 When any checkbox is tapped I want only that checkbox selected, but currently only last check box is selected. 当点击任何复选框时,我只希望选中该复选框,但当前仅选中最后一个复选框。 I've provided my current code below, please let me know if you see anything that should be changed. 我在下面提供了当前代码,如果您看到任何需要更改的内容,请告诉我。

Thanks. 谢谢。

- (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;
    }
}

I think your problem is that you are not using the right method, I think you need to implement the 我认为您的问题是您使用的方法不正确,我认为您需要实施

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

here you have the indexPath for the selected row, and the instance of the tableView, all you need to do is instantiate the TableViewCell and check your checkmark. 在这里,您具有选定行的indexPath和tableView的实例,您需要做的就是实例化TableViewCell并检查您的选中标记。

声明cellForRowAtIndexPath方法内的复选框按钮:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM