gpt4 book ai didi

ios - 滚动时 UITableView 奇怪的行为

转载 作者:行者123 更新时间:2023-11-29 02:37:14 25 4
gpt4 key购买 nike

我创建了一个 UITableView,其中包含显示 Users 的单元格。每个单元格都添加在此方法 -tableView:cellForRowAtIndexPath: 中。每个单元格都有链接到特定用户的内容,例如 UIImageViewUILabel

只要显示的单元格不超过 9-10 个,UITableView 就可以正常工作。但是当单元格的数量变多时,用户必须向下滚动才能查看所有单元格,这就是奇怪的行为开始的时候。来自第一、第二、第三等的内容被添加到单元格编号 11、12、13 等。然后当用户向上滚动时,应该位于第 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;

最佳答案

我通过将单元格标识符更改为唯一来解决我的问题。我不知道这是否真的是这样做的方法,或者这是否是一种好的做法,但是当我这样做时,它解决了我的问题。因此,最好提供一些反馈以了解这是否会导致我可能遗漏的任何其他问题?

NSString *identifier = [NSString stringWithFormat:@"Cell%li", indexPath.row];
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {
//My code..
}

关于ios - 滚动时 UITableView 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26206283/

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