Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么每次在iOS中滚动时都会取消选中UITable行_Iphone_Ios_Uitableview - Fatal编程技术网

Iphone 为什么每次在iOS中滚动时都会取消选中UITable行

Iphone 为什么每次在iOS中滚动时都会取消选中UITable行,iphone,ios,uitableview,Iphone,Ios,Uitableview,我设计了一个表视图,用于选择追随者列表发送消息, 所以在那里我可以选择一些追随者,然后在twitter上向他们发送消息 我正在将UITable View与UITableViewCellAccessoryCheckmark一起使用,以便 这个 但当我向下滚动表格时,选中的行每一次都会被取消选中 时间 我在FollowersListView.h @interface FollowersListView : UIViewController<UITableViewDelegate,UITabl

我设计了一个表视图,用于选择追随者列表发送消息, 所以在那里我可以选择一些追随者,然后在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
我想我需要根据已存储的选定列表加载每个单元格,但不知道如何操作

谁能告诉我哪里做错了, 任何帮助都将不胜感激

编辑

@诺瓦格


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

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

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


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

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

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


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

在cellForRowAtIndexPath中,您总是将其设置为未选中。您需要测试该行是否在所选跟随者列表中,如果是,请相应地设置附件


这一切都是由于单元重用,即使对于具有数千行的表,也可以保持较低的内存消耗。

在cellForRowAtIndexPath中,您总是将其设置为未选中。您需要测试该行是否在所选跟随者列表中,如果是,请相应地设置附件


这一切都是由于单元重用,即使对于具有数千行的表,也能保持较低的内存消耗。

问题是每次滚动时都会创建一个新单元。
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
)来记录已“检查”的名称。使用它,您可以在返回单元格之前添加以下内容:
cellforrowatinexpath:
方法:

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

下面是您应该在返回单元格之前添加的内容(之前未看到
列表SelectedFollowRSID
数组):

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

如果该行以前被点击过,则会将附件设置为选中标记。

问题是每次滚动时都会创建一个新单元格。
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
)来记录已“检查”的名称。使用它,您可以在返回单元格之前添加以下内容:
cellforrowatinexpath:
方法:

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

下面是您应该在返回单元格之前添加的内容(之前未看到
列表SelectedFollowRSID
数组):

cell.accessoryType=UITableViewCellAccessoryNone;
for(int i=0;i<[listSelectedFollowrsId计数];i++){
if([[listSelectedFollowrsId objectAtIndex:i]intValue]==indexath.row){
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
}
如果该行之前已点击,则会将附件设置为选中标记。

您可以使用此选项:

- (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;
}
您可以使用以下选项:

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

谢谢@apurv,但是给定的链接有简单的列表表,但是我想用chekc或unchekc行来完成谢谢@apurv,但是给定的链接有简单的列表表,但是我想用chekc或unchekc来完成rows@Novarg.. 是的,这就是我想知道的逻辑,我尝试了你的解决方案,但仍然没有得到应该存储在字典中的内容,顺便说一句,我在可变数组[DidSelectRowatineXpath]方法中存储了选定的行ID,你能清除我的PL吗?是的!!我真的很感激你的帮助。随时给我一个机会帮助你干杯@诺瓦格。。是的,这就是我想知道的逻辑,我尝试了你的解决方案,但仍然没有得到应该存储在字典中的内容,顺便说一句,我在可变数组[DidSelectRowatineXpath]方法中存储了选定的行ID,你能清除我的PL吗?是的!!我真的很感激你的帮助。随时给我一个机会帮助你干杯!!我给你举个例子,你可以从下载这个样本第二个标签中获取创意,你看到了检查和取消检查功能,我给你举个例子,你可以从下载这个样本第二个标签中获取创意,你看到了检查和取消检查功能,请告诉我你是如何获取twitter追随者的…@RDC我尝试了所有可能的方法,但是正在隐藏选定的元素,当我在表视图中滚动时,请务必让我知道您能告诉我您是如何完成的吗