繁体   English   中英

UITableView 单元格与背景图像

[英]UITableView cell with background image

我有一个 UITableView,其中有 3 张图像。 1 代表选定的 Cell,1 代表 Cell 背景,1 代表 TableView 背景。

我选择的单元格,工作得很好,但是普通单元格和 TableView 背景(向下/向上滚动时单元格后面的背景)有一些问题

有人可以帮我为每个单元格设置背景和 TableView 背景吗? 这是怎么做的?

正常细胞背景:(不确定是否正确)

// CELL'S BACKGROUND-IMAGE
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_normal.PNG"]];

选定的单元格背景:

// SELECTED CELL'S BACKGROUND-IMAGE
    UIView *viewSelected = [[[UIView alloc] init] autorelease];
    viewSelected.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"cell_highlighted.PNG"]];
    cell.selectedBackgroundView = viewSelected;

对于细胞

    cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];  
    cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_pressed.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

对于表视图

    [mEditTableView setBackgroundView:nil];
    [mEditTableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.png"]] ];

您可以设置UITableViewCell背景颜色/图像,通过以下委托方法

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
 *)indexPath  
{      
     if((indexPath.row)==0)  
       cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_normal.PNG"]]; //set image for cell 0

     if (indexPath.row==1)  
       cell.backgroundColor = [UIColor colorWithRed:.8 green:.6 blue:.6 alpha:1]; //set color for cell 1

     tableView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"cloud.jpg"]]; //set image for UITableView

  }

在 swift 中,您可以将背景图像设置为 UITableViewCell ,如下所示。

cell.backgroundView = UIImageView(image: UIImage(named: "yourImage.png")!)

把这段代码写在table view的这个function中。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

此图像将在每一行重复。

这设置了 tableView 的背景而不是单元格

// CELL'S BACKGROUND-IMAGE
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_normal.PNG"]];

尝试在 cellForRowAtIndexPath 中设置 cellbackground

 cell.backgroundColor =  [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_normal.PNG"]];

在 Swift 4 中,这有效:

cell.backgroundView = UIImageView.init(image: UIImage.init(named: "YORIMAGE.jpg"))

您可以尝试此代码块来设置 UITableviewCell 的背景图像

self.backgroundView = [[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]] autorelease];

self.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"background_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]] autorelease];

希望它对你有用。 :)

在 UITableview 单元格中设置图像

cell.imageView.image = [UIImage imageNamed:@"image.png"];

只有这个解决方案对我有用。

Cell.backgroundColor = [UIColor clearColor];
Cell.contentView.backgroundColor=[UIColor clearColor];

希望这可以帮助 !

//this concept is right but one problem is create when ur fastly scroll in uitable View then cell background color slowly change and indexing problem 

for Cell

    UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
    myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
    cell.selectedBackgroundView = myBackView;
    [myBackView release];

for table

    aTableView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"cloud.jpg"]];
    aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

//this concept is right but one problem is cteare when ur fastly scroll in uitable View then cell background color slowly change and indexing problem  so use this code

for Cell


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
   cell.backgroundColor = (indexPath.row%2)?[UIColor lightGrayColor]:[UIColor grayColor];
}

暂无
暂无

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

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