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”协议,只有数据源......所以它肯定不会进入委托方法。

确保它是这样的:

@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 回答