Static cells: Content does not appear

被刻印的时光 ゝ 提交于 2019-11-28 01:39:38

问题


I simply want to have a static table View with items in it. I used Xcode 4.2 with storyboard for this task. So I created a TableView with a tableViewController (subclass UITableViewController). I defined the content as "Static Cells" and style "Grouped".

After that I put some lables and all those objects I want to have in those cells. I created Outlets in the tableViewController:

#import <UIKit/UIKit.h>

@interface tableviewcontroller : UITableViewController
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmented;
@property (weak, nonatomic) IBOutlet UIImageView *imageview;
@property (weak, nonatomic) IBOutlet UITextView *text;

@end

put some content in the cell-label:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.label1.text = @"This is a title";
    self.label2.text = @"Annika";
    self.imageview.image = [UIImage imageNamed: @"picture.jpg"];
}

After build and run, it doesn't show me any of the elements... what did I do wrong?

Left: In storyboard, right: In the simulator.


回答1:


For static table views, you must not implement any of the datasource methods. The base implementation of UITableViewController has its own versions of these methods which return the appropriate content by reading the storyboard file. The methods are those which provide the table content, e.g. numberOfRows, numberOfSections, heightForRow, cellForRow and so forth.

In your case, you'd implemented tableView:cellForRowAtIndexPath: and returned empty cells each time. This meant the size of your cells was correct, but the content was gone.

Also, none of the outlets to content in your static table view will be set until after [super viewWillAppear:animated] has been called. They will still be nil at viewDidLoad, where you are calling them from, because the table view hasn't asked for any of it's content at that point.



来源:https://stackoverflow.com/questions/11238869/static-cells-content-does-not-appear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!