UIImageView显示在错误的UITableViewCell中

时间:2015-04-06 12:24:26

标签: ios objective-c uitableview parse-platform

我的UITableViewCell包含两个UIImageViewimgView1imgView2),基于我必须隐藏的数据源imgView2。它工作正常,期待一件事。在那些我需要隐藏imgView2的情况下,我会在滚动到底部后显示很多单元格imgView2。我需要以编程方式隐藏imgView2,因此我不明白导致此问题的原因。有可能解决这个问题吗?或者我应该创建另一个自定义单元格类来分隔两个布局?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

  NSString *contentCreator = [NSString stringWithFormat:@"%@", object[@"contentCreator"]];

MDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

if ([contentCreator isEqualToString:[PFUser currentUser].objectId]) {

    cell.usernameLabl.text = object[@"username"];

    PFFile *avatar1 = [Helper currentUserPhoto];
    cell.imageView2.layer.cornerRadius = cell.imageView2.frame.size.width / 2;
    cell.imageView2.clipsToBounds = YES;
    cell.imageView2.layer.borderWidth = 1.0f;
    cell.imageView2.layer.borderColor = [UIColor whiteColor].CGColor;
    cell.imageView2.file = senderAvatar;
    [cell.imageView2 loadInBackground];


    PFQuery *queryAvatar = [PFUser query];
    queryAvatar.cachePolicy = kPFCachePolicyCacheThenNetwork;
    [queryAvatar whereKey:@"objectId" equalTo:object[@"recipientUser"]];
    [queryAvatar getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {

        if (object) {
            PFFile *avatar2 = [object objectForKey:@"profilePhoto"];
            cell.imageView1.layer.cornerRadius = cellMessage.imageView1.frame.size.width / 2;
            cell.imageView1.clipsToBounds = YES;
            cell.imageView1.file = senderAvatar;

            [cell.imageView1 loadInBackground];
        }
    }];
}

else {

    cell.imageView2.hidden = YES;
    cell.usernameLabl.text = object[@"usernameSender"];

    PFQuery *queryAvatar2 = [PFUser query];
    queryAvatar2.cachePolicy = kPFCachePolicyCacheThenNetwork;
    [queryAvatar2 whereKey:@"objectId" equalTo:object[@"senderUser"]];
    [queryAvatar2 getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {

        if (object) {

            PFFile *senderAvatar = [object objectForKey:@"profilePhoto"];
            cell.imageView1.layer.cornerRadius = cell.imageView1.frame.size.width / 2;
            cell.imageView1.clipsToBounds = YES;
            cell.imageView1.file = senderAvatar;

            [cell.imageView1 loadInBackground];

        }
    }];


}
return cellMessage;

}

2 个答案:

答案 0 :(得分:2)

使用dequeueReusableCell的完整代码是什么?如果在分配它时出现nil,那么它是什么?

当单元格更多用于第一个/可见表格框架的单元格dequeueReusableCellWithIdentifier时,可能会发生:返回nil并且单元格被分配显示的imageView2,如果出列单元格被重复使用,则显示imageView2。

答案 1 :(得分:1)

我认为这是由dequeueReusableCellWithIdentifier:引起的故障。假设你为单元格imageView2隐藏x,然后向下滚动,用于x的单元格将重新用于另一个单元格'y',因此在单元格y中,imageView2仍然隐藏到期到你的单元格x的代码。

因此,最好的方法是在imageView2开头取消隐藏cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object 
{

  MDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

  cell.imageView2.hidden = NO;

  //rest of your code...