0

iOSアプリに問題があり、UITableViewCellのUILabelをラップします。テキストは表示されますが、1行にしか表示されません。

これが私のコードです:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    UILabel *noteLabel = (UILabel *)[cell.contentView viewWithTag:2];

    NSDictionary *contact = [jsonResults objectAtIndex:indexPath.row];
    noteLabel.lineBreakMode = UILineBreakModeWordWrap;
    noteLabel.numberOfLines = 0;
    noteLabel.text = [contact objectForKey:@"note"];   
    return cell;
}

問題はこの行に関係していると思います。

UILabel *noteLabel = (UILabel *)[cell.contentView viewWithTag:2];

タグからラベルを取得する他の方法がわかりません。この行では、ラベルのプロパティを変更できないと思います。

4

3 に答える 3

0

ラベルの高さを設定する必要がある場合があります。

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    UILabel *noteLabel = (UILabel *)[cell.contentView viewWithTag:2];

    NSDictionary *contact = [jsonResults objectAtIndex:indexPath.row];
    noteLabel.lineBreakMode = UILineBreakModeWordWrap;
    noteLabel.numberOfLines = 0;
    noteLabel.text = [contact objectForKey:@"note"];

    //Note: adjust the proper width to be constrained to
    CGSize size = [noteLabel.text sizeWithFont:noteLabel.font forWidth:150 lineBreakMode:noteLabel.lineBreakMode];
    CGRect labelFrame = noteLabel.frame;
    labelFrame.size = size;
    noteLabel.frame = labelFrame; 

    return cell;
}
于 2012-06-18T16:38:11.143 に答える
0

2012年から時間が経過しました。現在のXcode5の世界では、コードまたはInterface BuilderでnumberOfLinesが0に設定されている場合、高さは複数の行に対して自動的に拡張されます。このシナリオで複数の行が表示されないようにすることができる1つのことは、AutoLayoutの高さ制約がInterfaceBuilderまたはコードに設定されているかどうかです。このシナリオでは、切り捨てられたラベルのように見えますが、切り捨てられたラベルとは対照的に、省略記号(...)で終わることはありません。

AutoLayoutが期待どおりに機能するためには、UILabelに常に複数の行があることがわかっていない限り、通常はUILabelに高い高さを設定する必要はありません。

于 2014-07-27T20:42:54.683 に答える
0

私にとって違いを生んだのは、ラベルのpreferredMaxLayoutWidthを設定することでした。

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/#//apple_ref/occ/instp/UILabel/preferredMaxLayoutWidth

于 2015-06-25T01:15:06.457 に答える