Iphone UITableViewCellAccessory在滚动到屏幕外时消失

Iphone UITableViewCellAccessory在滚动到屏幕外时消失,iphone,ios,uitableview,ios4,Iphone,Ios,Uitableview,Ios4,我有一个充满对象的UITableView。在didSelectRowAtIndexPath方法中,我有一个UITableViewCellAccessoryCheckmark,选中该行时出现,取消选中时消失 以下是didSelectRowAtIndexPath方法的代码: 我还将此作为我的cellForIndexPath: 我的问题是,当所选单元格被滚动出视图时,与该值关联的复选标记现在在返回视图时消失 我该怎么做才能不让复选标记消失 谢谢当您滚动时,UITableViewCell将被回收。因此,

我有一个充满对象的UITableView。在didSelectRowAtIndexPath方法中,我有一个UITableViewCellAccessoryCheckmark,选中该行时出现,取消选中时消失

以下是didSelectRowAtIndexPath方法的代码:

我还将此作为我的cellForIndexPath:

我的问题是,当所选单元格被滚动出视图时,与该值关联的复选标记现在在返回视图时消失

我该怎么做才能不让复选标记消失


谢谢

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

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


这是进行设置的地方。调用此函数时,请检查单元格是否应显示复选标记,并相应地设置它

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


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

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

这是我发现最有效的方法:

(仅限“添加此项”和“结束添加此项”之间的任何代码)

另外,请确保将“(您的行数)”更改为返回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;
}
//在.h文件中添加此
NSMutableArray*索引数组;
//在.m文件中
-(无效)viewDidLoad{
//加上这个
indexArray=[[NSMutableArray alloc]init];
int i;
对于(i=0;i=0){
如果(比较==是){
}
否则{
compareButton.enabled=是;
UIImage*image=[UIImage ImageName:@“redbutton.png”];
[比较按钮设置图像:图像];
}
}
否则{
compareButton.enabled=否;
[比较按钮设置图像:nil];
[比较按钮setCustomView:nil];
}
}    
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“CustomCell”;
CustomCell*单元格=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
NSArray*topLevelObjects=[[NSBundle mainBundle]loadNibNamed:@“CustomCell”所有者:自选项:nil];
用于(topLevelObjects中的id currentObject){
if([currentObject IsKindof类:[UITableViewCell类]]){
单元格=(CustomCell*)currentObject;
打破
}
}
}
//正确设置单独的表。。。。
//加上这个
if([[indexArray objectAtIndex:indexPath.row]doubleValue]==1){
cell.accesoryType=UITableViewCellAccesoryType复选标记;
}否则{
cell.accesoryType=UITableViewCellAccesoryTypeNone;
}
//最后加上这个
返回单元;
}

适用于任何想要更通用方法和多个选定单元格的人

首先创建一个(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) ];

}

现在,每当您选择一个单元格时,都会添加一个复选标记,该indexPath.row存储在NSMutableArray中。取消选择该单元格时,将删除选中标记并将其从数组中删除。这意味着数组将只保存选中的单元格

然后,我们使用该数组在CellForRowatineXpath方法中为单元格提供正确的accessoryType。每当tableView需要一个单元格时,就会调用此方法,我们告诉它sell在创建时是否需要有复选标记

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

如果依赖用户点击选择行,如何在cellForRowAtIndexPath中设置复选标记?你能给我举个例子吗?当他们点击该行设置复选标记时,你需要将该信息存储在模型或控制器本身中。您可以做一些简单的事情,比如当用户单击一行时,通过索引路径将选中状态存储在字典中,然后使用它在cellForRowAtIndexPath中设置选中状态。
-(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) ];

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