gpt4 book ai didi

iphone - 为什么每次在 iOS 中滚动时 UITable 行都未选中

转载 作者:行者123 更新时间:2023-12-01 17:43:52 25 4
gpt4 key购买 nike

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

I am using UITable View with UITableViewCellAccessoryCheckmark to make this.



enter image description here

But when I scroll down table, the checked rows get unchecked every time.



我已为 中的选定关注者清除表格 View 和数组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 管理选定行的方法。

最佳答案

问题是每次滚动时都会创建一个新单元格。 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;
}
}

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

关于iphone - 为什么每次在 iOS 中滚动时 UITable 行都未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11898438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com