Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

ios - Static cells: Content does not appear

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. enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...