0

同様の質問がほぼ何十回も尋ねられていることがわかりますが、どの回答も私の問題を解決していません (または、疲れすぎて回答を正しくフォローしていません)。

カスタムセルを含むテーブルビューがあります。

セルはこんな感じ

ストーリーボードのカスタム セル

セルは、UITableViewCell から派生した ResultsViewCell 型です。

セルの最後のラベルは複数行セルです。そのセルは、次のセルの内容と重なる場合があります。

これが私の cellForRowAtIndexPath 関数です

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"StandardSearchResultCell";

        ResultsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        // Configure the cell...
        Data *theData = [Data getInstance];
        Company *theCompany = [theData.results objectAtIndex:indexPath.row];

        cell.lblTitle.text = theCompany.DisplayName;

        cell.lblDescription.text = theCompany.Description;
        cell.lblAddressPt1.text = theCompany.AddressPt1;
        cell.lblAddressPt2.text = theCompany.AddressPt2;
        cell.lblPhone.text = theCompany.Phone;
        cell.lblEmail.text = theCompany.Email;

        cell.lblDescription.adjustsFontSizeToFitWidth = false;
        cell.lblDescription.lineBreakMode = UILineBreakModeWordWrap;
        cell.lblDescription.numberOfLines = 0;
        [cell.lblDescription sizeToFit];

       ///////////////////////////////////////////
       //edit -Added after David H's answer, but it didn't solve the problem
       cell.contentView.clipsToBounds = false;
       UIFont *cellFont = [UIFont systemFontOfSize:12.0];
       CGSize constraintSize = CGSizeMake(270.0f, MAXFLOAT);
       CGSize labelSize = [theCompany.Description sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
       [cell.contentView setFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, 270, 90 + labelSize.height)];
       //end of edit
       ///////////////////////////////////////

        return cell;
    }


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Data *theData = [Data getInstance];
    Company *theCompany = [theData.results objectAtIndex:indexPath.row];

    UIFont *cellFont = [UIFont systemFontOfSize:12.0];
    CGSize constraintSize = CGSizeMake(270.0f, MAXFLOAT);
    CGSize labelSize = [theCompany.Description sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return (90.0f + labelSize.height);
}

私は何を間違っていますか?

4

3 に答える 3

1

驚かれるかもしれませんが、UIView プロパティ 'clipsToBounds' のデフォルトは NO です。つまり、サブビューがビューのフレームを超えて拡張されている場合でも表示します。

修正は、これらのビューのいずれも「clipsToBounds」が NO に設定されていないことを確認することです。cell.contentView で YES に設定し、cell.contentView のフレームがデリゲートの 'heightForRowAtIndexPath:' メソッドで報告した高さと同じであることを確認してください。

また、複数行のラベルで 'clipsToBounds' を YES に設定する必要があるかもしれません (確かではありません)。

于 2012-09-11T23:47:56.907 に答える
0

tableViewCellそのラベルのテキストの高さに基づいて高さを変更する必要があります。たとえば、次の方法を使用します。

- (int)textHeight:(NSString *)text {
    int width = self.tableView.frame.size.width;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 30)];
    label.numberOfLines = kMyNumberOfLines;

    //setup the label to look like the label of the cell

    label.text = text;
    [label sizeToFit];

    int h = (int)label.frame.size.height;
    [label release];
    return h;
}

次に、そのメソッドを使用しheightForRowAtIndexPathてセルの高さを決定します。

于 2012-09-12T00:53:20.527 に答える
0

このようにフォロー...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *myCell=[tableView dequeueReusableCellWithIdentifier:@"hi"];
    myCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hi"];

    if (myCell==nil)
    {
        NSArray *viewsToRemove = [mytable1 subviews];

        for (UITableView *table in viewsToRemove) 
        {
            [table removeFromSuperview];
        }
    }    
    return myCell;
}
于 2012-09-12T04:57:01.517 に答える