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

this is my code when ever i started scrolling the table towards downwards or upwards the first cell which i have checked have loosed the check mark sign, it is just hides all the way.. what is wrong with my code?

有帮助吗?

解决方案

Reuse Cell ,

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

其他提示

For selecting multiple rows:

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

}

//Cell for row at index path

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

}

//did select and deselect // I have used custom 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"]];

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top