多个自定义UITableViewCells全部变得相同

时间:2014-12-17 06:11:16

标签: ios objective-c uitableview twitter

我正在使用两个自定义UITableViewCell子类。它们都旨在保存Tweet的内容,一个特定于推文的文本,另一个特定的推文包含文本和图像。我在子类中以编程方式创建了两个单元格,并使用PureLayout进行自动布局以避免调整问题。

现在,当单元格中的推文最初加载时,一切都很好,有图像的那些看起来应该使用正确的子类,反之亦然,对于那些没有图像的人。但是当我开始滚动没有文本的单元格突然出现UIImageViews和其中的图像 - 即使这不是Tweet的实际图像,他们也会从其他推文中加载图像。

我不确定发生了什么,我相信这是我在装载和设置单元格的问题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

虽然我也使用SDWebImage来异步加载图像 - 但是它对于配置文件图像完全正常,加载不同图像时没有任何问题。这可能是因为TweetWithImageCell和TweetNormalCell都有个人资料图片。

任何人都可以了解最佳方法,使其正常工作吗?

代码如下:

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

    NSDictionary *tweet = [tweetsArray objectAtIndex:indexPath.row];

    if ([[tweet objectForKey:@"entities"] objectForKey:@"media"]) {
        if ([[[tweet objectForKey:@"entities"] objectForKey:@"media"]isKindOfClass:[NSArray class]]) {
            tweetMedia = [[tweet objectForKey:@"entities"] objectForKey:@"media"];
        } else {
        }
    }

    if ([tweetMedia objectAtIndex:0]) {
        //Initial cell setup
        StreamPhotoTableViewCell *cell = (StreamPhotoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifierPhoto];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if (cell == nil) {
            cell = tweetPhotoCell;
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }

        NSDictionary *tweetMediaFirst = [tweetMedia objectAtIndex:0];
        NSString *tweetMediaUrl = [tweetMediaFirst objectForKey:@"media_url"];
        [cell updateFonts];

        cell.titleLabel.text = [[tweet objectForKey:@"user"] objectForKey:@"name"];
        cell.userNameLabel.text = [NSString stringWithFormat:@"@%@",[[tweet objectForKey:@"user"] objectForKey:@"screen_name"]];
        cell.bodyLabel.text = [tweet objectForKey:@"text"];

        [cell.tweetImageView sd_setImageWithURL:[NSURL URLWithString:tweetMediaUrl]
                                 placeholderImage:[UIImage imageNamed:@"tweetImagePlaceholder"]];

        NSString *profileImageUrl = [[tweet objectForKey:@"user"] objectForKey:@"profile_image_url"];
        NSString *profileImageCheck = [profileImageUrl substringFromIndex: [profileImageUrl length] - 4];
        if([profileImageCheck isEqual:@".png"])
        {
            NSString *profileShort = [profileImageUrl substringToIndex:[profileImageUrl length] - 11];
            profileImageUrlBigger =  [NSString stringWithFormat:@"%@.png", profileShort];
        }
        else if([profileImageCheck isEqualToString:@"jpeg"])
        {
            NSString *profileShort = [profileImageUrl substringToIndex:[profileImageUrl length] - 12];
            profileImageUrlBigger =  [NSString stringWithFormat:@"%@.jpeg", profileShort];
        }
        else if([profileImageCheck isEqualToString:@".jpg"])
        {
            NSString *profileShort = [profileImageUrl substringToIndex:[profileImageUrl length] - 11];
            profileImageUrlBigger =  [NSString stringWithFormat:@"%@.jpg", profileShort];
        }
        [cell.profileImageView sd_setImageWithURL:[NSURL URLWithString:profileImageUrlBigger]
                       placeholderImage:[UIImage imageNamed:@"tweetImagePlaceholder"]];

        //Reply button setup/action
        [cell.replyButton addTarget:self action:@selector(replyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [cell.replyButton setAccessibilityHint:[[tweet objectForKey:@"user"] objectForKey:@"screen_name"]];

        //Retweet button setup/action
        if([[tweet objectForKey:@"retweeted"]  isEqual:@0]) {
            [cell.retweetButton addTarget:self action:@selector(retweetButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [cell.retweetButton setImage:[UIImage imageNamed:@"retweetIcon"] forState:UIControlStateNormal];
            [cell.retweetButton setAccessibilityHint:[tweet objectForKey:@"id_str"]];
        }
        else {
            [cell.retweetButton addTarget:self action:@selector(retweetedButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [cell.retweetButton setImage:[UIImage imageNamed:@"retweetedIcon"] forState:UIControlStateNormal];
            [cell.retweetButton setAccessibilityHint:[tweet objectForKey:@"id_str"]];
        }

        [cell setNeedsUpdateConstraints];
        [cell updateConstraintsIfNeeded];
        return cell;
    }
    else {
        //Initial cell setup
        StreamTableViewCell *cell = (StreamTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if (cell == nil) {
            cell = tweetCell;
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        [cell updateFonts];

        cell.titleLabel.text = [[tweet objectForKey:@"user"] objectForKey:@"name"];
        cell.userNameLabel.text = [NSString stringWithFormat:@"@%@",[[tweet objectForKey:@"user"] objectForKey:@"screen_name"]];
        cell.bodyLabel.text = [tweet objectForKey:@"text"];

        NSString *profileImageUrl = [[tweet objectForKey:@"user"] objectForKey:@"profile_image_url"];
        NSString *profileImageCheck = [profileImageUrl substringFromIndex: [profileImageUrl length] - 4];
        if([profileImageCheck isEqual:@".png"])
        {
            NSString *profileShort = [profileImageUrl substringToIndex:[profileImageUrl length] - 11];
            profileImageUrlBigger =  [NSString stringWithFormat:@"%@.png", profileShort];
        }
        else if([profileImageCheck isEqualToString:@"jpeg"])
        {
            NSString *profileShort = [profileImageUrl substringToIndex:[profileImageUrl length] - 12];
            profileImageUrlBigger =  [NSString stringWithFormat:@"%@.jpeg", profileShort];
        }
        else if([profileImageCheck isEqualToString:@".jpg"])
        {
            NSString *profileShort = [profileImageUrl substringToIndex:[profileImageUrl length] - 11];
            profileImageUrlBigger =  [NSString stringWithFormat:@"%@.jpg", profileShort];
        }
        [cell.profileImageView sd_setImageWithURL:[NSURL URLWithString:profileImageUrlBigger]
                                 placeholderImage:[UIImage imageNamed:@"tweetImagePlaceholder"]];

        //Reply button setup/action
        [cell.replyButton addTarget:self action:@selector(replyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [cell.replyButton setAccessibilityHint:[[tweet objectForKey:@"user"] objectForKey:@"screen_name"]];

        //Retweet button setup/action
        [cell.retweetButton setAccessibilityHint:[tweet objectForKey:@"id_str"]];
        if([[tweet objectForKey:@"retweeted"]  isEqual:@0]) {
            [cell.retweetButton addTarget:self action:@selector(retweetButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [cell.retweetButton setImage:[UIImage imageNamed:@"retweetIcon"] forState:UIControlStateNormal];
        }
        else {
            [cell.retweetButton addTarget:self action:@selector(retweetedButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [cell.retweetButton setImage:[UIImage imageNamed:@"retweetedIcon"] forState:UIControlStateNormal];
        }

        [cell setNeedsUpdateConstraints];
        [cell updateConstraintsIfNeeded];

        return cell;
    }
    return nil;
}

2 个答案:

答案 0 :(得分:0)

如果您的数组中包含元素,则此条件为真。因此,在这种情况下,else部分不会执行。

if ([tweetMedia objectAtIndex:0]) 

答案 1 :(得分:0)

奇怪的是,这似乎解决了这个问题:

if ([[[[tweetsArray objectAtIndex:indexPath.row] objectForKey:@"entities"] objectForKey:@"media"] objectAtIndex:0])

而不是已经分配了NSDictionary * tweet和NSMutableArray * tweetMedia的内容,并故意通过每个单元格的indexPath访问主tweetsArray。