iPhone UITableView PlainStyle with custom background image - done “entirely” in code

后端 未结 5 949
情深已故
情深已故 2021-02-09 08:28

I have been all over the place, seems the UITableView with a static background issue is well documented, but no one with a straight forward solution? Im building my

相关标签:
5条回答
  • 2021-02-09 08:47

    Thanks Coneybeare.

    It doesn't crash anymore and the background image turns up just perfect (along with my navigationController in the top) However, still no visible tableView? just the background image and the navigationControllerBar:

    This is my implementation:

    - (void)loadView {
    [super loadView];
    
    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
    [imageView setImage:[UIImage imageNamed:@"carbon_background.png"]];
    [self.view addSubview:imageView];
    
    [self.tableView = [[UIView alloc] initWithFrame:self.view.bounds] autorelease];
    
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.tableView];
    

    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView {
    return 1;
    

    }

    - (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section {
    return 3;
    

    }

    - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = @"Hello, World";
    
    return cell;
    

    }

    //commit edit, didSelectRow, memory … etc.

    The forum wasn't up for an entire .m file in one go. Please tell me if I left something out that could help indicate an error.

    I thought maybe it was the order of the layers and tried this without any luck:

        [self.view sendSubviewToBack:imageView];
    

    Hope I missed something obvious.

    Thanks for your time:)

    0 讨论(0)
  • 2021-02-09 08:49

    Ok, now it is running:) My tableView was not populated with my cells, so breakPointing through the thing I found out that even though I had implemented the TableViewDataSource and TableViewDelegate, this was only in the main view, I needed to set the delegate and datasource of the tableview to = self. For others seeking an answer to this here is the method as it ended up with Coneybeares help:

    - (void)loadView {
    [super loadView];
    
    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
    [imageView setImage:[UIImage imageNamed:@"carbon_background.png"]];
    [self.view addSubview:imageView];
    
    [self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    

    }

    0 讨论(0)
  • 2021-02-09 08:49

    Some tips:

    • If using a UISplitViewController:

      splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
      
    • If using a UINavigationController:

      navigationController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
      
    • If using both a UISplitViewController and a UINavigationController:

      navigationController.view.backgroundColor = [UIColor clearColor];
      splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
      

    Be sure to set the background color of any UIView you want to see through that is on top of the UINavigationController or UISplitViewController to [UIColor clearColor].

    0 讨论(0)
  • 2021-02-09 08:52

    All of this jumping through hoops is unnecessary if you're targeting > iOS 3.2, where you can use the new backgroundView property to set a UIImageView directly, e.g.:

    // In your UITableViewController subclass
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
        self.tableView.backgroundView = view;
    }  
    
    0 讨论(0)
  • 2021-02-09 09:03

    You need to set up your controller as a UIViewController, not a UITableViewController. Then add the tableview programmatically above a background imageView.

    @interface SomeController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
      ...
      UITableView *tableView;
      ...
    }
    @property (nonatomic, retain) UITableView *tableView;
    @end
    
    
    
    @implementation SomeController
    
    @synthesize tableView;
    
    ...
    
    - (void)loadView {
        [super loadView];
        UIImageView *v = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
        [v setImage:[UIImage imageNamed:@"table_background.png"]];
        [self.view addSubview:v];
    
    
        self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
        [self.tableView setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:self.tableView];
    }
    ...
    @end
    
    0 讨论(0)
提交回复
热议问题