4

現在、tableView を上にスクロールすると、奇妙で​​厄介なバグが見つかりました。「スクロール」を解放することにした場合、つまり「スクロール」をドロップしているため、「ビュー」は通常の位置に戻り、TableView のすべてのコンテンツが表示されます。一部のセルは、何らかの理由でサイズを変更できます。幅。なぜこれが発生するのか、何が問題なのかはわかりません。

私のセル (commentLabel) は、フォーラムのラベルの高さに応じて fitSize にカスタマイズされています。問題は、セルのコンテンツをカスタマイズしようとしている方法にあると思われます。関連するコードを投稿し、下の写真にも投稿します。

スクロールを上にドラッグする前に: http://tinypic.com/view.php?pic=2rfsum9&s=6

スクロールを通常の位置に放したりドロップした後。セルの 1 つが変更されました: http://tinypic.com/view.php?pic=swxnqv&s=6

コード:

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ForumthreadCell";
    UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Feedback *item = [self.items objectAtIndex:indexPath.row];

    UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
    UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
    UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

    [aliasLabel setText:item.alias];
    [commentLabel setText:item.comment];
    [dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];

    commentLabel.numberOfLines = 0;
    [commentLabel sizeToFit];

    return cell;

}

-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{

    CGSize maximumSize = CGSizeMake(labelWidth, 10000);

    //provide appropriate font and font size
    CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:12.0f]
                     constrainedToSize:maximumSize
                         lineBreakMode:UILineBreakModeTailTruncation];
    return labelHeighSize.height;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    Feedback *item = [self.items objectAtIndex:indexPath.row];

    CGFloat commentTextHeight = [self getLabelHeightForText:item.comment andWidth:162];
    return commentTextHeight + 39;

}

編集

新しいコード:

CGRect frame = commentLabel.frame;
frame.size.width = kLabelWidth;
frame.size.height = 10000;
commentLabel.frame = frame;

frame = commentLabel.frame;
frame.size.height += kOffset;
commentLabel.frame = frame;

しかし、commentLabel からのテキストが正しく表示されないので、私は何か間違ったことをしました。フレームを設定しようとする方法で誰かがここで私の過ちを犯すことはできますか? これは、セルの現在の外観ですhttp://oi50.tinypic.com/2w5u0s6.jpg

新しい完全なコード:

static const CGFloat kLabelWidth = 162;
static const CGFloat kOffset = 39;

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ForumthreadCell";
    UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Feedback *item = [self.items objectAtIndex:indexPath.row];

    UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
    UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
    UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

    [aliasLabel setText:item.alias];
    [commentLabel setText:item.comment];
    [dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];

    CGRect frame = commentLabel.frame;
    frame.size.width = kLabelWidth;
    frame.size.height = 10000;
    commentLabel.frame = frame;

    frame = commentLabel.frame;
    frame.size.height += kOffset;
    commentLabel.frame = frame;

    commentLabel.numberOfLines = 0;
    [commentLabel sizeToFit];

    return cell;

 }
4

1 に答える 1

3

セルを再利用する前に、ラベル フレームを調整する必要があります。あなたの場合、セルの高さを再利用しながら変更しています

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

ただし、セルの境界を超えないように、セルの高さに応じてラベルのフレームも調整する必要があります。

于 2012-11-05T07:20:58.247 に答える