swi*_*Boy 1 iphone uitableview ios
我设计了一个Table视图,For Select Followers列表发送消息,所以我可以选择一些关注者然后在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
Run Code Online (Sandbox Code Playgroud)
我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想我需要根据已经存储的选定列表加载每个单元格但不知道怎么做.
任何人都可以指导我做错的地方,任何帮助将不胜感激
编辑
@Novarg
我已将" listSelectedFollowrsId " 声明为.h文件中的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;
}
Run Code Online (Sandbox Code Playgroud)
它可能无法正常工作,我现在正在使用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;
Run Code Online (Sandbox Code Playgroud)
编辑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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果先前已经点过该行,则会将附件设置为复选标记.
| 归档时间: |
|
| 查看次数: |
2147 次 |
| 最近记录: |