表视图数据未显示

时间:2014-01-27 13:05:29

标签: ios uitableview

我创建了一个视图控制器,其上有2个视图。在顶视图中,我插入了一个表格视图,其中包含静态单元格和基本单元格。当我运行程序时,我手动输入到tableview的数据不会显示。

我的代码:

ViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface CallTeacherViewController : UIViewController <UITableViewDataSource,   UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *topLayer;

@property  (nonatomic) CGFloat layerPosition;

查看Controller.m

#import "CallTeacherViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface CallTeacherViewController ()

@end

@implementation CallTeacherViewController
@synthesize topLayer = _topLayer;
@synthesize layerPosition = _layerPosition;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

-(void)viewDidLoad {

    self.topLayer.layer.shadowOffset = CGSizeMake(-1,0);
    self.topLayer.layer.shadowOpacity = .9;
    self.layerPosition = self.topLayer.frame.origin.x;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

#define VIEW_HIDDEN 80

-(void) animateLayerToPoint:(CGFloat)x
{
    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationCurveEaseOut
                     animations:^{
                         CGRect frame = self.topLayer.frame;
                         frame.origin.x = x;
                         self.topLayer.frame = frame;
                     }
                     completion:^(BOOL finished){
                         self.layerPosition =self.topLayer.frame.origin.x;                         
                     }];
}

- (IBAction)toggleLayer:(id)sender {
    if (self.layerPosition == VIEW_HIDDEN) {
        [self animateLayerToPoint:0];
    } else {
        [self animateLayerToPoint:VIEW_HIDDEN];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if (self.layerPosition == VIEW_HIDDEN) {
        [self animateLayerToPoint:0];
    } else {
        [self animateLayerToPoint:0];
    }
}

- (IBAction)panLayer:(UIPanGestureRecognizer*)pan {
    if (pan.state == UIGestureRecognizerStateChanged) {
        CGPoint point  = [pan translationInView:self.topLayer];
        CGRect frame = self.topLayer.frame;
        frame.origin.x = self.layerPosition + point.x;
        if (frame.origin.x < 0 ) frame.origin.x = 0;
        self.topLayer.frame = frame;
    }

    if (pan.state == UIGestureRecognizerStateEnded) {
        if (self.topLayer.frame.origin.x <= 81) {
            [self animateLayerToPoint:0];
        } else {
            [self animateLayerToPoint: VIEW_HIDDEN];
        }
    }
}

3 个答案:

答案 0 :(得分:2)

这里用动态tableview内容填充你的tableview。如果您真的希望通过故事板获得静态内容,请参阅Ilario回答。

// H

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *topLayer;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

//米

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // if you forget these two lines the methods below won't fire.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.items = @[@"Foo", @"Bar", @"Test"]; // whatever data you need
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [self.items objectAtIndex:indexPath.row];

    return cell;
}

答案 1 :(得分:1)

如果您在故事板中设置了静态单元格的文本,则必须删除此方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

如果您从故事板设置静态单元格,则不需要这些方法,因为他们决定在故事板上:有多少单元格和每个单元格的文本

修改

如果你想在UIViewController中使用带有静态单元格的tableView,而你的应用程序是iOS 6+,你可以:

在你的UIVIewController和容器视图中添加UIContainerView,你可以把UITableViewController放到静态单元格

enter image description here

enter image description here

答案 2 :(得分:0)

TableViewController课程下添加以下代码,适合我

    override func viewDidAppear(animated: Bool){
        self.tableView.reloadData() 
    }