作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我设计了一个表格 View ,用于选择关注者列表发送消息,
所以我可以在那里选择一些关注者,然后在 Twitter 上向他们发送消息。
I am using UITable View with UITableViewCellAccessoryCheckmark to make this.
But when I scroll down table, the checked rows get unchecked every time.
@interface FollowersListView : UIViewController<UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *tableFollowers;
NSMutableArray *listFollowrsName;
NSMutableArray *listSelectedFollowrsId;
}
@property(nonatomic,retain) NSMutableArray *listFollowrsName;
@property(nonatomic,retain) NSMutableArray *listSelectedFollowrsId;
@end
- (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);
}
}
最佳答案
问题是每次滚动时都会创建一个新单元格。 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;
}
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;
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/
我是一名优秀的程序员,十分优秀!