7

tableView を追加し、テーブル ビュー セルをそこにドラッグしました。ユーティリティ パネルで、スタイルを字幕に変更しました。私はまた、コードでそれを変更しようとしました:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter;

    cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [myArray2 objectAtIndex:indexPath.row];

    return cell;

中央揃えがうまくいかない!

回避策として、セルにラベル オブジェクトを追加しようとしました。しかし、私はそれにアクセスする方法がわかりません。アウトレットを割り当てても、これは機能しません。

cell.labelForCell....

私は何をすべきか?セルなどにラベルを追加せずに、通常の方法で機能させる方法に関する提案はありますか?

4

6 に答える 6

19

UITableViewCellStyleSubtitleテキストの配置は変更できないため、ラベルを付けて配置を追加する必要があります. そのためには、このコードを使用できます.

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *myLabel;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        myLabel = [[UILabel alloc] initWithFrame:Your_Frame];
        //Add a tag to it in order to find it later
        myLabel.tag = 111;
        //Align it
        myLabel.textAlignment= UITextAlignmentCenter;

        [cell.contentView addSubview:myLabel];
    }

    myLabel = (UILabel*)[cell.contentView viewWithTag:111];
    //Add text to it
    myLabel.text = [myArray objectAtIndex:indexPath.row];

    return cell;
}
于 2012-06-16T10:48:42.643 に答える
0

その理由は次のとおりだと思います: textLabel の幅はテキストの長さに依存します。テキストが長すぎてシグナル行にすべてを表示することができず、既に改行モードを設定して行数を 0 に設定している場合は、テキストの配置が機能することがわかります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *identifier = @"identifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
        cell.textLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.textColor = [UIColor redColor];
        cell.detailTextLabel.textColor = [UIColor greenColor];
        cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
        cell.textLabel.numberOfLines = 0;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"You can initialize a very long string for the textLabel, or you can set the font to be a large number to make sure that the text cann't be shown in a singal line totally:%d", indexPath.row];
    return cell;

}

于 2012-06-16T14:55:41.643 に答える
0

フレーム調整が効くと思います。私はUITableViewCellStyleValue2のスタイルを持っていましたが、textLabelに少し長いテキストがある場合、末尾が切り捨てられ、ここでtextAlignmentが機能しないため、幅のtextLabelを増やすことを考えました.

 -(void)layoutSubviews{
        [super layoutSubviews];

        if ([self.reuseIdentifier isEqualToString:@"FooterCell"]) {
            CGRect aTframe  = self.textLabel.frame;
            aTframe.size.width += 40;
            self.textLabel.frame = aTframe;

            CGRect adTframe  = self.detailTextLabel.frame;
            adTframe.origin.x += 70;
            self.detailTextLabel.frame = adTframe;        
        }
    }
于 2014-06-23T13:25:55.057 に答える
0

こんにちは、コードの代わりに次を追加してください。

cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;

への変更

cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.detailTextLabel.textAlignment = NSTextAlignmentCenter;

それはうまくいきます。

于 2014-07-24T11:59:29.210 に答える
0

cellForRowAtIndexPath: メソッドでUITableViewCellのtextLabeldetailTextLabelのフレームを変更することはできません。

セルをサブクラス化したくない場合は、次を使用して小さなハックを実装できます

performSelector:withObject:afterDelay:

デフォルトの layoutSubviewsの直後にジオメトリを変更するためのゼロ遅延:

[self performSelector:@selector(alignText:) withObject:cell afterDelay:0.0];

詳細はこちら

于 2014-07-19T14:55:40.073 に答える