表格视图中没有数据显示在表格单元格中

时间:2014-07-11 11:54:18

标签: objective-c uitableview

当我运行该代码时,我在该代码中遇到问题,然后在tableview中没有数据显示在它的表格单元格中。控制台中没有错误但数据未显示。请指导我做了那个代码的错误。

InvoiceCell.h

#import <UIKit/UIKit.h>

@interface InvoiceCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *ItemImage;
@property (strong, nonatomic) IBOutlet UILabel *ItemName;
@property (strong, nonatomic) IBOutlet UILabel *ItemType;
@property (strong, nonatomic) IBOutlet UILabel *date;
@property (strong, nonatomic) IBOutlet UILabel *Amount;
@property (strong, nonatomic) IBOutlet UILabel *status;

@end

InvoiceCell.m

#import "InvoiceCell.h"

@implementation InvoiceCell
@synthesize ItemImage,ItemName,ItemType,Amount,status;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    // Initialization code
    }
    return self;
}

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

// Configure the view for the selected state
}

@end

InvoiceViewController.h

#import <UIKit/UIKit.h>

@interface InvoiceViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) NSArray *DataImages;
@property (nonatomic, strong) NSArray *DataName;
@property (nonatomic, strong) NSArray *Datatype;
@property (nonatomic, strong) NSArray *DataAmount;
@property (nonatomic, strong) NSArray *DataStatus;
@property (nonatomic, strong) NSArray *Datadate;


@end

InvoiceViewController.m

#import "InvoiceViewController.h"
#import "InvoiceCell.h"

@interface InvoiceViewController ()

@end

@implementation InvoiceViewController
@synthesize DataImages,DataName,Datatype,DataAmount,DataStatus,Datadate;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    DataImages = @[@"img1.jpg", @"img2.jpg", @"img3.jpg", @"img4.jpg", @"img5.jpg"];
    DataName = @[@"Suit", @"Shirt", @"paint", @"Suit", @"Suit"];
    Datatype = @[@"Brown", @"Black", @"Blue", @"Black", @"Blue"];
    DataAmount = @[@"$500", @"$400",@"$500", @"$400",@"$300"];
    DataStatus = @[@"Paid", @"Due", @"Paid", @"Due", @"Paid"];
    Datadate = @[@"8/5/2014", @"10/6/2014",@"8/5/2014", @"10/6/2014",@"8/5/2014"];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   // Return the number of sections.
   return 1;
}

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

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

    if (cell==nil)
    {
        cell = [[InvoiceCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }
    long row = [indexPath row];

    NSLog(@"%@", cell.ItemName.text = DataName[row]);
    cell.ItemName.text = DataName[row];
    cell.ItemType.text = Datatype[row];
    cell.ItemImage.image = [UIImage imageNamed:DataImages[row]];
    cell.date.text = Datadate[row];
    cell.Amount.text = DataAmount[row];
    cell.status.text = DataStatus[row];

    return cell;

}

enter image description here enter image description here

在调试窗口和控制台中显示数据,但不在tableview单元格中的模拟器屏幕上显示数据

谢谢。

1 个答案:

答案 0 :(得分:0)

您的StoryBoard中可能有一个tableView,但您的代码中没有与它连接。将其添加到InvoiceViewController.h

@property IBOutlet UITableView *tableView;

并将其连接到Interface Builder中的TableView。

然后你可以在InvoiceViewController.m中设置DataSource和Delegate - [viewDidLoad]方法

_tableView.delegate = self;
_tableView.dataSource = self;

现在,TableView将知道调用您实现的委托方法,您应该在表中看到您的数据。