2013-02-15 38 views
0

我的问题是,当我选择一个单元格时,它也会自动选择其他单元格。 另外,当我取消选中所选单元格时,自动选中的单元格也会被取消选择。 任何帮助,非常感谢。 谢谢。在UITableView中自动选择错误的单元格?

我的代码是:

#pragma mark 
#pragma mark UITableViewDelegate 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [cellDataArray count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *cellIdentifier = @"customCellSearchBySpeciality"; 
    SpecialityCustomCell *cell = (SpecialityCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SearchBySpecialityViewController" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.cellData.text = [cellDataArray objectAtIndex:indexPath.row]; 
    return cell; 
} 


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

    SpecialityCustomCell *selectedCell = (SpecialityCustomCell *)[tableView cellForRowAtIndexPath:indexPath]; 

    if (selectedCell.selectedCellImageView.image) { 
     selectedCell.selectedCellImageView.image = nil; 
    } 
    else{ 
     [selectedCell.selectedCellImageView setImage:[UIImage imageNamed:@"image"]]; 
    } 
} 

回答

1

我认为你需要保持其细胞的你已经在各行aaded然后做选择和取消细胞的imageview的像下面的那个特定对象的情况下,

NSMutableArray *cellArray; 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *cellIdentifier = @"customCellSearchBySpeciality"; 
    SpecialityCustomCell *cell = (SpecialityCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    [cellArray addObject:cell]; 

    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SearchBySpecialityViewController" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.cellData.text = [cellDataArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    for(int cc=0;cc<[cellArray count];cc++){ 
    if(indexPath.row==cc){ 
    if ([cellArray objectAtIndex:cc].selectedCellImageView.image) { 
     [cellArray objectAtIndex:cc].selectedCellImageView.image = nil; 
    } 
    else{ 
     [[cellArray objectAtIndex:cc].selectedCellImageView setImage:[UIImage imageNamed:@"image"]]; 
    } 
    } 
} 
+0

@BhusahnVU你在cellarray中添加的细胞对象是否为零? – 2015-07-17 10:37:45

相关问题