我创建了一个UITableView
,其中包含显示Users
的单元格。在此方法-tableView:cellForRowAtIndexPath:
内添加每个单元格。并且每个单元格都有与特定用户链接的内容,如UIImageView
和UILabel
。UITableView滚动时的奇怪行为
只要没有超过9-10个单元显示,UITableView
就能正常工作。但是,当细胞数量变得更高时,用户必须向下滚动以查看全部细胞,这是奇怪行为开始时的情况。来自第一,第二,第三等的内容被添加到十一,十二,十三等单元格中。然后,当用户然后向上滚动时,应该在第11,12,13的内容现在在第一,第二和第三单元中...
我希望有人了解我的问题,并且知道什么是错在这里..
这里是我的代码用户添加细胞..虽然忽略解析的东西,我不认为这是相关
- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
if (tableview == commentViewTableView) {
//Ignore this
} else if (tableview == tableView) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 34, 34)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
[cell addSubview:imageView];
UILabel *usernameLabel = [[UILabel alloc] initWithFrame:CGRectMake(44, 0, 160, 44)];
usernameLabel.textAlignment = NSTextAlignmentLeft;
usernameLabel.font = [UIFont systemFontOfSize:17];
usernameLabel.backgroundColor = [UIColor clearColor];
[cell addSubview:usernameLabel];
UIImageView *hitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(245, 9.5, 25, 25)];
hitImageView.contentMode = UIViewContentModeScaleAspectFill;
hitImageView.clipsToBounds = YES;
hitImageView.image = [UIImage imageNamed:@"hit.png"];
[cell addSubview:hitImageView];
NSString *key = //Code to retrieve userKey
PFQuery *query = [PFUser query];
[query whereKey:@"objectId" equalTo:key];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
[[object objectForKey:@"image1"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
NSString *ageString = [[NSString alloc] initWithFormat:@"%li", (long)age];
imageView.image = [UIImage imageWithData:data];
usernameLabel.text = [NSString stringWithFormat:@"%@, %@", [object objectForKey:@"username"], ageString];
}
}];
}
}];
}
}
return cell;
}
请问您可以发布代码吗?特别是'tableView:cellForRowAtIndexPath:' – nburk 2014-10-05 19:20:20
对于不了解单元重用的人来说,这是一个非常普遍的问题。由于滚动时单元格会被重用,因此您需要设置cellForRowAtIndexPath中的所有内容。 – rdelmar 2014-10-05 19:26:39
我更新了代码,只是忽略Parse.com的东西,这是不相关的,我认为 – Peter 2014-10-05 19:28:39