【发布时间】:2015-09-30 23:00:56
【问题描述】:
在一个简单的列表应用程序 (iOS) 中,我的 tableViewCells 遇到了一些非常奇怪的行为。
这是我的工作应用程序的基本功能:
TableView 有 2 个部分。第一部分(例如,带有 2 个单元格)始终可见。第二部分可以使用部分标题中的自定义按钮显示/隐藏。我创建了两个类(即 FirstSectionItem 和 SecondSectionItem),它们都具有布尔属性“已完成”和其他一些属性。
编译后,应用程序按预期运行。点击单元格会显示复选标记,再次点击它们会隐藏复选标记(=自定义 imageView)。然而,在点击一些不同的单元格(随机顺序)或显示/隐藏第二部分之后,无论我点击多少单元格,复选标记(ImageViews)都会保持选中状态。一段时间后,每个单元格都被选中并且不能取消选中,但布尔值仍在不断变化。
下面是部分代码:
@property NSMutableArray *sectionTitleArray;
@property NSMutableDictionary *sectionContentDict;
@property NSMutableArray *firstSectionItems;
@property NSMutableArray *secondSectionItems;
ViewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.sectionTitleArray) {
self.sectionTitleArray = [NSMutableArray arrayWithObjects:@"section1", @"section2", nil];
}
self.firstSectionItems = [[NSMutableArray alloc] init];
self.secondSectionItems = [[NSMutableArray alloc] init];
[self loadInitialData];
self.sectionContentDict = [[NSMutableDictionary alloc] init];
self.arraySection1 = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.firstSectionItems count]; i++)
{
FirstSectionItem *firstSectionItem = [self.firstSectionItem objectAtIndex:i];
[self.arraySection1 addObject:firstSectionItem.itemName];
}
self.arraySection2 = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.secondSectionItems count]; i++)
{
SecondSectionItem *secondSectionItem = [self.secondSectionItems objectAtIndex:i];
[self.arrayFuture addObject:secondSectionItem.itemName];
}
[self.sectionContentDict setValue:self.arraySection1 forKey:[self.sectionTitleArray objectAtIndex:0]];
[self.sectionContentDict setValue:self.arraySection2 forKey:[self.sectionTitleArray objectAtIndex:1]];
}
CellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
// ... cell content ...
NSArray *content = [self.sectionContentDict valueForKey:[self.sectionTitleArray objectAtIndex:indexPath.section]];
//... constraints ...
UIImage *checkmark = [UIImage imageNamed:@"checkmark.png"];
UIImage *noCheckmark = [UIImage imageNamed:@"transparent.png"];
UIImageView *imageView = [[UIImageView alloc] init];
if (indexPath.section==0){
FirstSectionItem *firstSectionItem = [self.firstSectionItems objectAtIndex:indexPath.row];
imageView.image = firstSectionItem.completed ? checkmark : noCheckmark;
}
if (indexPath.section==1){
if(self.sec2isTapped == YES){
SecondSectionItem *secondSectionItem = [self.secondSectionItems objectAtIndex:indexPath.row];
imageView.image = secondSectionItem.completed ? checkmark : noCheckmark;
}
}
[cell.contentView addSubview:imageView];
// ... more constraints
return cell;
}
didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if (indexPath.section ==0){
FirstSectionItem *tappedItem = [self.firstSectionItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
}
if (indexPath.section ==1){
SecondSectionItem *tappedItem = [self.secondSectionItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
}
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
我尝试了多种方法,但似乎都没有奏效。我无法弄清楚问题是什么。
(我是 Objective C 的新手,所以有些部分可能看起来有点曲折)。
提前致谢。
【问题讨论】:
标签: ios objective-c iphone uitableview uiimageview