UITableView Image

I am trying to customize a table view with a list that scrolls. Background is a repeating image of clouds.

As a preliminary attempt to implement this, I used the following code:

- (void)viewDidLoad { aTableView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"cloud.jpg"]]; aTableView.separatorStyle = UITableViewCellSeparatorStyleNone; } 

However, this is not quite the way I had hoped. The image is placed as a background in each cell (which is good), and also as a background to represent the table, so when the user scrolls past the borders of the screen and uses a rebound, the same cloud background is displayed. This gives an undesirable overlap effect.

I would like each cell to have this repeating image (so that it scrolls along with the table), but for the background, so that the table view is in plain black. Is it possible?

I know that I can add an image to a custom cell and send it back, but this seems like too much workaround.

Greetings


I managed to sort it using the following code:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { [cell setBackgroundColor:[UIColor colorWithPatternImage: [UIImage imageNamed: @"cloud.jpg"]]]; } 

Is this the best way to install UIColor as a UIImage or is this resource more intensive?

+4
source share
3 answers

I had exactly the same problem, except for half the implementation path, we decided to abandon the design and instead switched to user-defined table cells. Our original design should have looked like a notepad with cells on each line.

Subclassing UITableViewCell may seem like a daunting task, but it's worth it if you need to configure the camera in the future and it looks like it will do what you request. I can post the code for you from my project if you're interested. However, keep in mind that you will not have a background on the UITableView itself, only on the cells.

EDIT: sending code related to creating a custom UITableViewCell: My cell is called HotOffersCell, This is HotOffersCell.h

 #import <UIKit/UIKit.h> @interface HotOffersCell : UITableViewCell { IBOutlet UILabel *mainLabel; IBOutlet UILabel *subLabel; IBOutlet UILabel *price; IBOutlet UIImageView *imageView; } @property (nonatomic, retain) UILabel *mainLabel; @property (nonatomic, retain) UILabel *subLabel; @property (nonatomic, retain) UILabel *price; @property (nonatomic, retain) UIImageView *imageView; @end 

HotOffersCell.m is very simple, just sysnthesize for all properties, nothing more. Then in the TableView method of your table view controller:

 // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"HotOffersCell"; HotOffersCell *cell = (HotOffersCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HotOffersCell" owner:nil options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell class]]) { cell = (HotOffersCell *) currentObject; break; } } } NewsItem *newsItem = [newsList objectAtIndex:indexPath.row]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"d MMM YY"]; NSString *date = [formatter stringFromDate:newsItem.departure_date]; if (indexPath.row % 2 == 0) { cell.contentView.backgroundColor = [UIColor colorWithWhite:230.0/255 alpha:1]; // Match Colour } else { cell.contentView.backgroundColor = [UIColor colorWithWhite:242.0/255 alpha:1]; // Match Colour } cell.mainLabel.text = newsItem.destination; cell.subLabel.text = [[NSString alloc] initWithFormat:@"%@ - %@ nights", date, newsItem.nights]; if ([self.navigationController.title isEqualToString:@"Top 20"]) { cell.price.text = newsItem.price_inside; } else { cell.price.text = newsItem.price_balcony; } cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", newsItem.cruise_line]]; [formatter release]; return cell; } 

I also have HotOffersCell.xib. This file contains a UITableViewCell and there is no view, I linked all the corresponding shortcuts and nothing else. It provides a convenient graphical way to edit placements on a label without changing the code, I will even let my brother change this =)

I hope this helps

+2
source

You set the background to a UITableView . Not a UITableViewCell background.

0
source

Source: https://habr.com/ru/post/1301387/


All Articles