将UITableView添加到现有的UIViewController

时间:2013-07-25 12:53:21

标签: objective-c uitableview uiviewcontroller

我有一个UIViewController打开相机,在imagePickerController:didFinishPickingMediaWithInfo:之后显示UITableView

不知何故未触发tableView委托代码。 tableView显示在我的iPhone上,但是有空行。似乎只显示IB Stuff而没有代码。但我无法弄清楚我错过了什么。

如何让UITableView正常工作?

MainStoryboard.storyboard

Camera View Controller Scene
----------------------------
  Camera View Controller
    View
      Image View // hooked up with .h
      Table View  // hooked up with .h
        Table View Cell - clocation

CameraViewController.h

#import <UIKit/UIKit.h>

@interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView; // hooked up with IB UIImageView
@property (strong, nonatomic) NSMutableArray *locations;
@property (strong, nonatomic) IBOutlet UITableView *tableView;  // hooked up with IB UITableView

@end

CameraViewController.m(已更新20130725,带有工作解决方案)

#import "CameraViewController.h"

@interface CameraViewController ()

@end

@implementation CameraViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // self.locations = [[NSMutableArray alloc] init];  
    // is filled with content when photo is taken. Means has content when tableview shall be shown. 
    self.locations = @[ @"foo", @"bar" ]; // dummy setup for this thread

    // Do any additional setup after loading the view.
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            // select Photo
    } else {
        [self takePhoto];
    }

    [self initTableView];

} 

// [...] Camera Stuff


#pragma mark - table view things

-(void)initTableView {
    _tableView.dataSource = self;
    _tableView.delegate = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.locations.count;  // a breakpoint is configured but never get hit...
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"clocation";

    // Not working in this case: 
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    // You have to use this one: 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"Empty Cell %d", indexPath.row];

    return cell;
}

0 个答案:

没有答案