8

テキストを右揃えにする必要があります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"lisn"];
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"lisn"] autorelease];
CGSize  textSize = { 210.0, 10000.0 };
CGSize size = [[gMessageArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

UILabel *lisnerMessage=[[[UILabel alloc] init] autorelease];
lisnerMessage.backgroundColor = [UIColor clearColor];
[lisnerMessage setFrame:CGRectMake(75 ,20,size.width + 5,size.height+2)];
lisnerMessage.numberOfLines=0;
lisnerMessage.textAlignment=UITextAlignmentRight;
lisnerMessage.text=[gMessageArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:lisnerMessage];
return cell
}

しかし、私のテキストは正しく配置されていません助けてください

4

4 に答える 4

14

sizeWithFontを使用してからフレームをそのサイズに設定しているため、テキストは正しく配置されます。ラベルに明るい灰色の背景色を追加して、私が話していることを確認してください。ラベルはテーブルセルと同じサイズに設定し、テキストがその中に流れるようにする必要があります。次に、右に揃えます。

サンプルで更新

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lisn"];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"lisn"];

    UILabel *lisnerMessage = [[UILabel alloc] init];
    lisnerMessage.backgroundColor = [UIColor clearColor];
    [lisnerMessage setFrame:cell.frame];
    lisnerMessage.numberOfLines = 0;
    lisnerMessage.textAlignment = NSTextAlignmentRight;
    lisnerMessage.text = [gMessageArray objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lisnerMessage];

    return cell
}
于 2012-06-27T14:36:57.567 に答える
1

最後に、問題を修正しました。小さなミスをしていた

[lisnerMessage setFrame:CGRectMake(75 ,20,size.width + 5,size.height+2)];

size.widthテキストが右揃えになった後、特定の座標 200 を削除して指定します。

[lisnerMessage setFrame:CGRectMake(75 ,20,200,size.height+2)];

ご回答ありがとうございます

于 2012-06-28T07:34:39.550 に答える
0

インターフェイスビルダー/ストーリーボードでラベルを作成し、「右揃え」オプションを選択してみませんか? 次に、それを lisnerMessage という名前のプロパティとして接続します。これにより lisnerMessage.text=[gMessageArray objectAtIndex:indexPath.row]; 、記述しているコードの量が大幅に削減され、確実に機能します。

于 2012-06-27T14:44:50.537 に答える