gpt4 book ai didi

iphone - UITableViewCell 背景 View /图像

转载 作者:行者123 更新时间:2023-12-03 20:30:45 27 4
gpt4 key购买 nike

在 cellForRowAt 中的哪里设置 tableview 单元格的背景图像..我该怎么做?这就是我的做法,它有点有效,但我想知道设置背景图像(cell.backgroundView)的正确方法以及它是否位于if(cell==nil)内的正确位置) 或在“if”之外。?

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

UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"]];
}

return cell;
}

最佳答案

过去经常这样做,但现在我更喜欢 willDisplayCell:

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

文档说

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

使用 cellForRowAtIndexPath 时,存在一些奇怪的特殊情况,其中单元格自定义无法完全按照我的意愿工作。 willDisplayCell 没有这样的问题。

关于iphone - UITableViewCell 背景 View /图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13828643/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com