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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if(getIndex<21)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];

return cell;
}

这是我的代码,当我开始向下或向上滚动表格时,我检查过的第一个单元格已松开复选标记符号,它只是一直隐藏..我的代码有什么问题?

4

2 回答 2

2

重用细胞,

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


        int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if(getIndex<21)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    } // END HERE
    cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
    return cell;
}
于 2013-10-03T12:42:29.190 回答
2

用于选择多行:

- (void)addTableView {

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake("whatever frame you want to set")
                                                            style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


self.timeSheetEntryTableView.allowsMultipleSelection = YES;

self.array = [[NSMutableArray alloc]init];
[self.view addSubview:self.tableView];

}

//索引路径处的行的单元格

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

NSString *cellIdentifier = @"Cell Identifier";


self.cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];


if (!self.cell) {

    self.cell = [[Cell alloc]initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:cellIdentifier];
    self.cell.accessoryType = UITableViewCellAccessoryNone;

}
self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([self.array containsObject:indexPath])
{
    [self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];
}
else
{
     [self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];
}

return self.cell;

}

//选择和取消选择//我使用了自定义单元格

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

self.cell = [tableView cellForRowAtIndexPath:indexPath];

[self.array addObject:indexPath];
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];

}

- (void)tableView:(UITableView *)tableView 
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

self.cell = [tableView cellForRowAtIndexPath:indexPath];

[self.array removeObject:indexPath];

[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];

}
于 2014-04-01T04:59:04.917 回答