1

私の目標は、背景、フォント、サイズを使用して、独自のセルスタイルを定義することです。これやってみます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ScreenListElements *currentScreenElement = [self.screenDefBuild.elementsToTableView objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:currentScreenElement.objectName];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:currentScreenElement.objectName];

    }

    cell.textLabel.text = currentScreenElement.objectName;

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{  
cell.contentView.backgroundColor = [UIColor blackColor];
}

セルの背景を変更したいのですが、アプリがwillDisplayCellメソッドに入りません。私は自分のクラスを次のように宣言しました:

@interface MyTableView : UIViewController  <UITableViewDataSource,NSCopying> {
}

他に何かあるべきですか?または、独自のセルスタイルを宣言するためのより良い方法かもしれません。

4

4 に答える 4

6
@interface MyTableView : UIViewController  <UITableViewDataSource, UITableViewDelegate ,NSCopying> {
}

メソッド

tableView:willDisplayCell:forRowAtIndexPath

のデリゲートメソッドですUITableView

編集済み(正解で受け入れられるようにするため)

デリゲートの設定

[myTableView setDelegate:self];
于 2012-04-23T15:05:52.810 に答える
0

わかりました。「UITableViewDelegate」プロトコルを実装しておらず、dataSourceのみを実装していることがわかりました。したがって、デリゲートメソッドは確実に入力されません。

それがそのようであることを確認してください:

@interface MyTableView : UIViewController  <UITableViewDataSource, UITableViewDelegate ,NSCopying> {
}
于 2012-04-24T07:02:24.253 に答える
0

UITableviewCell委任プロトコルを確認してください:

- (void)tableView:(UITableView *)tableView 
        willDisplayCell:(UITableViewCell *)cell 
        forRowAtIndexPath:(NSIndexPath *)indexPath{  

    cell.contentView.backgroundColor = [UIColor blackColor];

}

于 2014-10-17T05:51:34.407 に答える
0

すべてを正しく設定した場合と同じ問題が発生しましたが、関数を手動で記述していると、構文が欠落し、このエラーが発生する可能性があります。

(Instance method 'tableView(tableView:willDisplayCell:forRowAtIndexPath:)tableView(_:willDisplay:forRowAt:)プロトコルのオプション要件にほぼ一致UITableViewDelegate

このように、関数は認識されません。関数を正しく記述してください。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)

デリゲートがselfこれに割り当てられている場合は機能します。そうでない場合は、コードを記述したときにエラーが表示されます。

于 2021-09-02T07:53:27.890 に答える