简体   繁体   English

UITableViewCell背景视图/图像

[英]UITableViewCell background view/image

Where do I set backgroundImage of tableview cell in cellForRowAt.. And how do I do that ? 如何在cellForRowAt。中设置tableview单元格的backgroundImage。我该怎么做? This is how I did it and it kinda works, but I want to know is that right method for setting backgroundimage ( cell.backgroundView ) and is it right place inside if(cell==nil) or outside that "if".? 这就是我做到的方式,并且有点奏效,但是我想知道是设置backgroundimage( 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;
}

Used to do that, but nowadays I prefer willDisplayCell : 曾经这样做,但是现在我更喜欢willDisplayCell

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

Documentation says 文件说

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. 委托返回后,表视图将仅设置alpha和frame属性,然后仅在为行滑动时为其设置动画效果。

When using cellForRowAtIndexPath, there were some weird special cases, where cell customization just didn't work fully as I wanted. 当使用cellForRowAtIndexPath时,有一些奇怪的特殊情况,其中单元格自定义无法完全按照我的要求进行。 No such problems with willDisplayCell. willDisplayCell没有这种问题。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath {  
[cell.imageview setImage:[UIImage imageNamed:@"image1.png"]];
   }
return cell;

} }

try with this might be helpful... 尝试这样做可能会有所帮助...

- (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];

         UIImageView *backimg = [[UIImageView alloc] init];
        backimg.frame = CGRectMake(0, 0, 320, 44);
        backimg.tag = 20;
        [cell.contentView addSubview:backimg];
    }
    UIImageView *imag = (UIImageView *)[cell.contentView viewWithTag:20];
    [imag setImage:[UIImage imageNamed:@"sky.png"]];
    return cell;
}

The best place to set a background view for a cell is the tableView:willDisplayCell:forRowAtIndexPath: method. 设置单元格背景视图的最佳位置是tableView:willDisplayCell:forRowAtIndexPath:方法。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM