3

セルに字幕テキストを含むテーブルビューを作成しようとしています

問題は、字幕テキストの配置を右に設定しようとすると機能しませんが、メインテキストでは正常に機能することです。

これが私のコードです

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomCell";

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

    [[cell textLabel] setTextAlignment:UITextAlignmentRight];
    [[cell detailTextLabel] setTextAlignment:UITextAlignmentRight];

    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.detailTextLabel.text = @"test";
    return cell;
}

字幕コードを削除すると、位置合わせは正常に機能します

何か案が ?

4

2 に答える 2

5

わかりましたので、UITableView Cell をサブクラス化し、init でラベルをカスタマイズします。layoutSubviews をオーバーライドして、ラベルを右に移動できます。

- (void)layoutSubviews {
    [super layoutSubviews];
    self.textLabel.frame = CGRectMake(0.0, 68.0, 80.0, self.frame.size.height);
    self.detailTextLabel.frame = CGRectMake(0.0, 68.0, 120.0, self.frame.size.height);
}

これらは単なる例の値であり、アイデアを得ることができます。

于 2012-06-01T14:29:06.780 に答える
0

セルを 2 回初期化する理由:

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

2 つのラベルを持つカスタム セルを作成し、これらのラベルに配置できます。次の手順に従います。

1. UITableViewCell の新しいファイル サブクラス、たとえば labelCustomCell を追加します。2.labelCustomCell で、label1 と label2 という 2 つのラベルを作成します。3. initWithStyle メソッドでこれらのラベルを割り当て、配置を提供します。4.layoutSubViews メソッドで、これらのラベルにフレームを割り当てます。5. cellForRowAtIndexPath メソッドで、次のコードを記述します。

    static NSString *CellIdentifier = @"DataEntryCell";
                labelCustomCell *cell = (labelCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (cell == nil) {
                    cell = [[[labelCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                }
cell.label1.text = [array objectAtIndex:indexPath.row];
    cell.label1.font = [UIFont systemFontOfSize:18];
    cell.label2.text = @"test";

labelCustomCell をインポートすることを忘れないでください。

于 2012-06-01T14:32:55.457 に答える