UITableView从故事板加载错误的原型单元格

时间:2013-09-30 00:19:59

标签: uitableview uistoryboard

我有一个非常奇怪的错误,我无法弄清楚,我现在真的需要一些帮助。基本上,tableView有三个UITableViewCell子类,它们有自己的原型单元格,并出现在它们自己的部分中。但是第三部分中的原型也出现在第二部分中。奇怪的是,在将一些NSLog放入我的代码之后,第二部分中显示的单元格(应该在第三部分中)是UITableViewCell的正确子类,但是来自另一个原型的内容正在显示。我不知道为什么会这样。我检查确保原型是正确的子类,标识符都是正确的,它们是。以前有人遇到过这个问题吗?提前告诉我的cellForRowAtIndexPath:方法有多长:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WaveDetailCell *waveDetails;
    ActionsCell *actions;
    CommentCell *comments;
    id cellToReturn;

    if (self.hasAgrees || self.hasComments)
    {
        switch (indexPath.section)
        {
            case 0:
            {
                waveDetails = [tableView dequeueReusableCellWithIdentifier:WaveDetailCellIdentifier forIndexPath:indexPath];

                if ([waveDetails.waveLabel respondsToSelector:@selector(setAttributedText:)])
                {
                    NSString *name = self.currentWaveObject.wavedToUserID;
                    NSString *fullWaveString = [name stringByAppendingString:self.currentWaveObject.waveString];
                    NSRange range = [fullWaveString rangeOfString:self.currentWaveObject.wavedToUserID];
                    UIFont *font = [UIFont fontWithName:HelveticaMedium size:16.0f];
                    NSDictionary *attrs = @{NSFontAttributeName: font};
                    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullWaveString];
                    [attributedString addAttributes:attrs range:range];
                    [waveDetails.waveLabel setAttributedText:attributedString];
                }

                waveDetails.wavedByNameLabel.text = self.currentWaveObject.wavedByUserID;
                waveDetails.timestampLabel.text = self.currentWaveObject.creationDate;

                //round the corners of the imageview
                [self setCornerRadiusForImageView:waveDetails.profilePicImageView withRadius:CGSizeMake(4.0f, 4.0f)];
                [self setCornerRadiusForImageView:waveDetails.waverProfileImageView withRadius:CGSizeMake(2.0f, 2.0f)];
                cellToReturn = waveDetails;
            }
                break;

            case 1:
            {
                if (self.hasAgrees)
                {
                    //create agrees cell
                }
                else
                {
                    actions = [tableView dequeueReusableCellWithIdentifier:ActionsCellIdentifier forIndexPath:indexPath];
                    cellToReturn = actions;
                }
            }

            case 2:
            {
                if (self.hasAgrees)
                {
                    //if there are agrees, and no comments, then the last section should be the actions cell
                    actions = [tableView dequeueReusableCellWithIdentifier:ActionsCellIdentifier forIndexPath:indexPath];
                    cellToReturn = actions;
                }
                else
                {
                    //there are comments, and no agrees
                    comments = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];
                    CommentObject *comment = [self.currentWaveObject.commentsArray objectAtIndex:indexPath.row];
                    comments.userNameLabel.text = comment.commenterID;
                    comments.commentLabel.text = comment.commentText;
                    comments.timeStampLabel.text = comment.timeStamp;
                    [self setCornerRadiusForImageView:comments.userImageView withRadius:CGSizeMake(3.0f, 3.0f)];
                    cellToReturn = comments;
                }
            }

            default:
                break;
        }
    }
    else if (self.hasComments && self.hasAgrees)
    {
        switch (indexPath.section)
        {
            case 0:
            {
                waveDetails = (WaveDetailCell *)[tableView dequeueReusableCellWithIdentifier:WaveDetailCellIdentifier forIndexPath:indexPath];

                if ([waveDetails.waveLabel respondsToSelector:@selector(setAttributedText:)])
                {
                    NSString *name = self.currentWaveObject.wavedToUserID;
                    NSString *fullWaveString = [name stringByAppendingString:self.currentWaveObject.waveString];
                    NSRange range = [fullWaveString rangeOfString:self.currentWaveObject.wavedToUserID];
                    UIFont *font = [UIFont fontWithName:HelveticaMedium size:16.0f];
                    NSDictionary *attrs = @{NSFontAttributeName: font};
                    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullWaveString];
                    [attributedString addAttributes:attrs range:range];
                    [waveDetails.waveLabel setAttributedText:attributedString];
                }

                waveDetails.wavedByNameLabel.text = self.currentWaveObject.wavedByUserID;
                waveDetails.timestampLabel.text = self.currentWaveObject.creationDate;

                //round the corners of the imageview
                [self setCornerRadiusForImageView:waveDetails.profilePicImageView withRadius:CGSizeMake(4.0f, 4.0f)];
                [self setCornerRadiusForImageView:waveDetails.waverProfileImageView withRadius:CGSizeMake(0.5f, 0.5f)];
                cellToReturn = waveDetails;
            }
                break;

            case 1:
            {
                //create agrees cell
            }
                break;

            case 2:
            {
                actions = (ActionsCell *)[tableView dequeueReusableCellWithIdentifier:ActionsCellIdentifier forIndexPath:indexPath];
                cellToReturn = actions;
            }
                break;

            case 3:
            {
                comments = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];
                CommentObject *comment = [self.currentWaveObject.commentsArray objectAtIndex:indexPath.row];
                comments.userNameLabel.text = comment.commenterID;
                comments.commentLabel.text = comment.commentText;
                comments.timeStampLabel.text = comment.timeStamp;
                [self setCornerRadiusForImageView:comments.userImageView withRadius:CGSizeMake(2.0f, 2.0f)];
                cellToReturn = comments;
            }
                break;

            default:
                break;
        }
    }
    else
    {
        if (indexPath.row == 0)
        {
            waveDetails = (WaveDetailCell *)[tableView dequeueReusableCellWithIdentifier:WaveDetailCellIdentifier forIndexPath:indexPath];

            if ([waveDetails.waveLabel respondsToSelector:@selector(setAttributedText:)])
            {
                NSString *name = self.currentWaveObject.wavedToUserID;
                NSString *fullWaveString = [name stringByAppendingString:self.currentWaveObject.waveString];
                NSRange range = [fullWaveString rangeOfString:self.currentWaveObject.wavedToUserID];
                UIFont *font = [UIFont fontWithName:HelveticaMedium size:16.0f];
                NSDictionary *attrs = @{NSFontAttributeName: font};
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullWaveString];
                [attributedString addAttributes:attrs range:range];
                [waveDetails.waveLabel setAttributedText:attributedString];
            }

            waveDetails.wavedByNameLabel.text = self.currentWaveObject.wavedByUserID;
            waveDetails.timestampLabel.text = self.currentWaveObject.creationDate;

            //round the corners of the imageviews
            [self setCornerRadiusForImageView:waveDetails.profilePicImageView withRadius:CGSizeMake(4.0f, 4.0f)];
            [self setCornerRadiusForImageView:waveDetails.waverProfileImageView withRadius:CGSizeMake(0.5f, 0.5f)];
            cellToReturn = waveDetails;
        }
        else
        {
            actions = (ActionsCell *)[tableView dequeueReusableCellWithIdentifier:ActionsCellIdentifier forIndexPath:indexPath];
            cellToReturn = actions;
        }
    }
    return cellToReturn;
}

1 个答案:

答案 0 :(得分:0)

等等,我明白了。这很令人尴尬,但我在switch语句中的一个案例中忘记了break