1

我设计了一个表格视图,用于选择关注者列表以发送消息,因此我可以在那里选择一些关注者,然后在 Twitter 上向他们发送消息。

我正在使用 UITable View 和 UITableViewCellAccessoryCheckmark 来做到这一点。

在此处输入图像描述

但是当我向下滚动表格时,每次都未选中已检查的行。

我在FollowersListView.h中为选定的追随者清除了表格视图和数组

@interface FollowersListView : UIViewController<UITableViewDelegate,UITableViewDataSource>{

IBOutlet UITableView *tableFollowers;

NSMutableArray *listFollowrsName;   
NSMutableArray *listSelectedFollowrsId;
}

@property(nonatomic,retain) NSMutableArray *listFollowrsName;    
@property(nonatomic,retain) NSMutableArray *listSelectedFollowrsId;

@end

我的 tableview 代码在FollowersListView.m文件中是这样的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [listFollowrsName count];
}



- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"CellWithSwitch"];
    //if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithSwitch"] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];

    //cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.accessoryType = UITableViewCellStyleDefault;
    //}

    NSString *cellValue = [listFollowrsName objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;    

    return cell;
}

//to select a row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];


    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;        
        NSLog(@"row is un-checked %@",[NSNumber numberWithInt:path.row]);
        [listSelectedFollowrsId removeObject:[NSNumber numberWithInt:path.row]];
        NSLog(@"Now selected person are%@",listSelectedFollowrsId);

    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        NSLog(@"row is checked %@",[NSNumber numberWithInt:path.row]);
        [listSelectedFollowrsId addObject:[NSNumber numberWithInt:path.row]];       
        NSLog(@"Now selected person are%@",listSelectedFollowrsId);
    }

}

我想我需要根据已经存储的选定列表加载每个单元格,但不知道该怎么做。

任何人都可以指导我做错了什么,任何帮助将不胜感激

编辑

@诺瓦格

我已在 .h 文件中将“ listSelectedFollowrsId ”声明为 NSMutable 数组,并在 viewDidLoad 方法中初始化,然后在didSelectRowAtIndexPath方法中添加/删除值以管理选定的行。

4

4 回答 4

5

问题是每次滚动时都会创建一个新单元格。条件必须存在,否则您将if (cell == nil)一遍又一遍地重写单元格。

尝试以下操作:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"CellWithSwitch"];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithSwitch"] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.accessoryType = UITableViewCellStyleDefault;
  }
  NSString *cellValue = [listFollowrsName objectAtIndex:indexPath.row];
  cell.textLabel.text = cellValue;    
  return cell;
}

它可能不起作用,我现在使用的是Windows机器,所以现在无法编译代码并自己测试它。

希望能帮助到你

编辑

如果它不能解决问题,我还会添加一个变量(可能是一个NSMutableDictionary)来记录已“检查”的名称。return cell;使用它,您可以在方法之前添加以下内容cellForRowAtIndexPath:

if ([[theDictionary objectForKey:/*your key here, can be anything*/]isEqualToString:/*string or whatever, just make sure that the checked ones are different from the unchecked ones*/])
  cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
  cell.accessoryType = UITableViewCellAccessoryNone;

编辑 2

这是您应该在之前添加的内容return cell;(之前没有看到listSelectedFollowrsId数组):

cell.accessoryType = UITableViewCellAccessoryNone;
for (int i = 0; i < [listSelectedFollowrsId count]; i++) {
  if ([[listSelectedFollowrsId objectAtIndex:i]intValue] == indexPath.row) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
}

如果该行之前已被点击,这会将附件设置为复选标记。

于 2012-08-10T09:07:23.860 回答
1

你可以使用这个:

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

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

    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];        
        cell.backgroundColor=[UIColor clearColor];        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;      
        cell.accessoryType=UITableViewCellAccessoryNone;            
    }
    else
    {
        UIView *subview;
        while ((subview= [[[cell  contentView]subviews]lastObject])!=nil)       
            [subview removeFromSuperview];            
    }

    NSString *cellValue = [listFollowrsName objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;    
    return cell;
}
于 2012-08-10T09:23:29.247 回答
0

这是因为当您滚动表格时,会调用 cellForRowAtIndexPath。

在此功能中,您将一次又一次地创建单元格。因此,您在 didSelectRowAtIndexPath 中标记的复选标记将被删除。

有关 tableview 的详细示例,请参阅此链接

于 2012-08-10T09:04:28.703 回答
0

在 cellForRowAtIndexPath 中,您始终将其设置为未选中。您需要测试该行是否在您选择的关注者列表中,如果是,请相应地设置附件。

这完全是由于单元重用以保持较低的内存消耗,即使对于具有数千行的表也是如此。

于 2012-08-10T09:06:21.377 回答