表格视图仍然空白?

时间:2014-10-11 17:01:01

标签: ios objective-c xcode uitableview

我的tableview有问题。我正在尝试制作一个程序,从我的数据库中下载作业详细信息并列出它们。 tableview仍为空白,视图已连接到特定控制器,我已检查JSON提要和单元标识符是否正常。我的单元格行也很奇怪,虽然我已经正确对齐了tableview对象,但行线仍在右边缘上继续。我在之前的项目中遇到了类似的问题,但不知道出了什么问题。

以下是代码的一部分(来自:http://codewithchris.com/iphone-app-connect-to-mysql-database/

JobViewer.m

@interface JobViewer()
{
    NSMutableData *_downloadedData;
}
@end

@implementation JobViewer

- (void)downloadItems
{
    // Getting json file
    NSURL *jsonFileURL = [NSURL URLWithString:@"here is my correct URL"];

    //Making request
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileURL];

    //Creating NSURLConnection with the request
    [NSURLConnection connectionWithRequest:urlRequest delegate:self];

}

#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //Initialize data
    _downloadedData = [[NSMutableData alloc] init];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //Append the data that was downloaded
    [_downloadedData appendData:data];

}

- (void)connectionDidFinishDownloading:(NSURLConnection *)connection
{
    //Creating array for the information
    NSMutableArray *_jobs = [[NSMutableArray alloc] init];

    // Parsing the JSON file
    NSError *error;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];

    //Looping through JSON objects and storing them into array
    for (int i = 0; i < jsonArray.count; i++)
    {
        NSDictionary *jsonObject = jsonArray[i];

        //Creating a new object and getting data from JSON element
        Job *newJob = [[Job alloc] init];
        newJob.jobid = jsonObject[@"Job_id"];
        newJob.companyid = jsonObject[@"Company_id"];
        newJob.customer = jsonObject[@"Customer"];
        newJob.phone = jsonObject[@"Phone"];
        newJob.email = jsonObject[@"Email"];
        newJob.address = jsonObject[@"Address"];
        newJob.city = jsonObject[@"City"];
        newJob.header = jsonObject[@"Header"];
        newJob.notes = jsonObject[@"Notes"];
        newJob.date = jsonObject[@"Date"];
        newJob.driver = jsonObject[@"Driver"];

        //Added question to array
        [_jobs addObject:newJob];
    }

    // Passing the done data and passing items back
    if (self.delegate)
    {
        [self.delegate itemsDownloaded:_jobs];
    }

}

@end

MainViewController.m

@interface MainViewController ()

{
     JobViewer *_jobViewer;
     NSArray *_listObjects;
}

@end

@implementation MainViewController

- (void)viewDidLoad {

    [super viewDidLoad];
     self.mainList.delegate = self;
     self.mainList.dataSource = self;

     _listObjects = [[NSArray alloc] init];
     _jobViewer = [[JobViewer alloc] init];
     _jobViewer.delegate = self;
     [_jobViewer downloadItems];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

 - (void)itemsDownloaded:(NSArray *)items
 {

     _listObjects = items;
     [self.mainList reloadData];

 }

 #pragma mark Table View Delegate Methods
// You may remove this method
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

     return _listObjects.count;

 }

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

     NSString *cellIdentifier = @"InfoCell";
     UITableViewCell *newCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

     Job *item = _listObjects[indexPath.row];

     newCell.textLabel.text = item.address;

     //[newCell setBackgroundColor:[UIColor blackColor]];

     return newCell;

 }

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

2 个答案:

答案 0 :(得分:0)

您可能需要在故事板中添加“InfoCell”作为重用标识符。

答案 1 :(得分:0)

您的tableView可能无法识别单元格,因此您应该在故事板中的tableView处添加单元格,并将其称为“ InfoCell

如果您不想在故事板中将单元格添加到tableView,并且您的单元格中包含xib。你可以在你的viewController中调用这个方法:

[tableView registerNib:[UINib nibWithNibName:@"IndoCell" bundle:nil] forCellReuseIdentifier:@"IndoCell"];

如果你的笔尖被称为“IndoCell.xib