2011-04-29 40 views
2

我有一个UITableView充满了对象。在didSelectRowAtIndexPath方法我有一个UITableViewCellAccessoryCheckmark当行被选中时出现,并在未选中时消失。UITableViewCellAccessory消失滚动关闭屏幕

继承人为didSelectRowAtIndexPath方法方法的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
UITableViewCell *curCell = [beerTable cellForRowAtIndexPath:indexPath]; 

if (curCell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    [curCell setAccessoryType:UITableViewCellAccessoryNone]; 
    compareCount = (compareCount - 1); 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
     NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; 
     [compareBeers removeObject:[searchResults objectAtIndex:indexPath.row]]; 
     [compareCarbs removeObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; 
    } 
    else { 
     [compareBeers removeObject:[beerNames objectAtIndex:indexPath.row]]; 
     [compareCarbs removeObject:[carbAmount objectAtIndex:indexPath.row]]; 
    } 

} 
else { 
    [curCell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    compareCount = (compareCount + 1); 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
     NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; 
     [compareBeers addObject:[searchResults objectAtIndex:indexPath.row]]; 
     [compareCarbs addObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; 
    } 
    else { 
     [compareBeers addObject:[beerNames objectAtIndex:indexPath.row]]; 
     [compareCarbs addObject:[carbAmount objectAtIndex:indexPath.row]]; 
    } 

} 

if (compareCount > 0) { 
    if (compareOn == YES){ 
    } 
    else { 
    compareButton.enabled = YES; 
    UIImage *image = [UIImage imageNamed:@"redbutton.png"]; 
    [compareButton setImage:image]; 
    } 
} 

else { 
    compareButton.enabled = NO; 
    [compareButton setImage:nil]; 
    [compareButton setCustomView:nil]; 
} 

} 

我也有这个作为我cellForIndexPath

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


static NSString *CellIdentifier = @"CustomCell"; 

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

    for (id currentObject in topLevelObjects){ 
     if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell = (CustomCell *) currentObject; 
      break; 
     } 
    } 
} 

// Setting separate tables correctly.... 


return cell; 
} 

我的问题是,当所选择的单元格滚动在视图之外,与该值相关联的复选标记现在消失。

我应该怎么做才能使对号消失?

谢谢

回答

0

UITableViewCell在您滚动时被回收。因此,当您再次滚动tableView时,您看到的单元格可能与以前可见的单元格不同。

您需要每次在cellForRowAtIndexPath中重置单元格的状态。您的代码有此评论区域 //正确设置单独的表格......

这是您进行设置的位置。当这个被调用时,检查单元格是否应该显示复选标记,并相应地设置它

4

当您滚动浏览数据(这就是dequeueReusableCellWithIdentifier所做的)时,您的单元格将被重新使用。在didSelectRowAtIndexPath中有复选标记的单元格将被回收用于其他行,并且不再与已检查的行有任何连接。

您需要设置/取消设置cellForRowAtIndexPath中的附件视图,以便在检查的行滚动回视图时,它们会得到适当的检查。

+0

如果我依靠用户点击选择行,我如何设置cellForRowAtIndexPath中的复选标记?你能否介绍一下我的例子? – 2011-04-29 02:01:26

+1

当他们点击该行设置复选标记时,需要将该信息存储在模型或控制器本身中。你可以做一些简单的事情,例如当用户点击一行时,将检查的状态存储在字典中的索引路径,然后使用它在cellForRowAtIndexPath中设置检查的状态。 – 2011-04-29 02:09:30

0

尝试将选定索引存储在数组或字典中用于didSelectRow方法中所选单元格以及cellForRowAtIndexPath检查数组中是否存在该索引,然后使用设置accesoryType选中单元格。希望你能理解

0

这是我找到最好的工作:

(刚之间的任何代码“加入这个”和“结束添加此”)

此外,还要确保你改变“(您的号码ofRows)“指向一个返回int的对象,或者是一个int本身。如myCustomArray.count,或24

//In .h file add this 
NSMutableArray *indexArray; 

//in .m file 

- (void)viewDidLoad { 
    //Add this 
    indexArray = [[NSMutableArray alloc] init]; 
    int i; 
    for (i = 0; i <= (Your Number ofRows); i++) { 
     [indexArray addObject:[NSNumber numberWithDouble:0]]; 
    } 
NSLog(@"indexArray = %@",indexArray);//You can check the console now to see if you have an array of "zeros" 
    //End Add this 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
UITableViewCell *curCell = [beerTable cellForRowAtIndexPath:indexPath]; 


if (curCell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    [curCell setAccessoryType:UITableViewCellAccessoryNone]; 
    compareCount = (compareCount - 1); 

    //Add this 
    [indexArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithDouble:0]]; 
    //End Add this 


    if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
     NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; 
     [compareBeers removeObject:[searchResults objectAtIndex:indexPath.row]]; 
     [compareCarbs removeObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; 
    } 
    else { 
     [compareBeers removeObject:[beerNames objectAtIndex:indexPath.row]]; 
     [compareCarbs removeObject:[carbAmount objectAtIndex:indexPath.row]]; 
    } 

} 
else { 
    [curCell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    compareCount = (compareCount + 1); 

    //Add this 
    [indexArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithDouble:1]]; 
    //End Add this 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
     NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; 
     [compareBeers addObject:[searchResults objectAtIndex:indexPath.row]]; 
     [compareCarbs addObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; 
    } 
    else { 
     [compareBeers addObject:[beerNames objectAtIndex:indexPath.row]]; 
     [compareCarbs addObject:[carbAmount objectAtIndex:indexPath.row]]; 
    } 

} 

if (compareCount > 0) { 
    if (compareOn == YES){ 
    } 
    else { 
     compareButton.enabled = YES; 
     UIImage *image = [UIImage imageNamed:@"redbutton.png"]; 
     [compareButton setImage:image]; 
    } 
} 

else { 
    compareButton.enabled = NO; 
    [compareButton setImage:nil]; 
    [compareButton setCustomView:nil]; 
} 

}  



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


static NSString *CellIdentifier = @"CustomCell"; 

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

    for (id currentObject in topLevelObjects){ 
     if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell = (CustomCell *) currentObject; 
      break; 
     } 
    } 
} 

// Setting separate tables correctly.... 

//Add this 
if ([[indexArray objectAtIndex:indexPath.row] doubleValue] == 1) { 
    cell.accesoryType = UITableViewCellAccesoryTypeCheckmark; 
} else { 
    cell.accesoryType = UITableViewCellAccesoryTypeNone; 
} 
//End Add this 

return cell; 
} 
1

任何人想要一个更通用的方法和用于多个选择的细胞

首先创建(NSMutableArray的*)了selectedCells来跟踪所选择的单元格。 下实现2种委托方法

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

//add the checkmark to cell when selected 
if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryNone){ 
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 

} 

//once selected add that selected cell to the selectedCells array 

[self.selectedCells addObject:@(indexPath.row) ]; 

} 

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

//set the accessory type back to none 
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){ 
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 

//remove the selected cells index from the selectedCells array 
[self.selectedCells removeObject:@(indexPath.row) ]; 

} 

现在,当你永远选择添加勾选细胞和indexPath.row存储到一个NSMutableArray。当您选择该单元格时,它将删除该复选标记并将其从数组中移除。这意味着数组只会保存被检查的单元格。

然后,我们使用该数组为单元格cellForRowAtIndexPath方法提供正确的accessoryType。每次tableView需要一个单元时,我们都会调用这个方法,我们告诉它它的销售需要在创建或不创建时有一个复选标记。

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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

//Create an NSNumber to hold the row of the cell to be created 
    NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row]; 

//then ask the array if the selectedCells array has that object. 
//if it does then that cell needs a checkmark when created. 

if ([self.selectedCells containsObject:rowNsNum]){ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
else { cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    [cell.textLabel setText:@"your contents"]; 
} 

return cell; 
}