2014-10-11 71 views
0

我有一个问题,我的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 
+1

“行线继续在右边”这很有意思;那是什么意思?我不知道你认为“排队”是什么;你可以发布图片并提供链接吗? – matt 2014-10-11 17:05:19

+0

嗨!这可能只是我的英语不好,但这里是“行”的图片:http://jessetakkinen.kapsi.fi/public_html/rows.png – 2014-10-11 17:16:35

+0

一般来说,我NSLog数据并硬编码一个字典输出的第一行让我了解dfmuir提到的问题。 – timpone 2014-10-11 17:38:05

回答

0

您可能需要添加“InfoCell”作为Storyboard中的重用标识符。

+0

感谢您的评论。如果您的意思是属性检查器中的标识符字段,我已经将其设置为“InfoCell”。 – 2014-10-11 18:08:50

0

可能是你的tableView无法识别电池,所以你应该在故事板tableView增加该小区,它被称为“InfoCell

如果你不希望添加到细胞中tableView的故事板,你有一个xib与你的手机。你可以在你的viewController调用这个方法:

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

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

相关问题