2013-10-18 56 views
1

爲了設置UITableView單元格的字體,textLabel和顏色,iOS 7中已經棄用了很多方法。我也很難用這些值填充視圖。這裏是我的代碼片段:在iOS 7中使用NSArray填充UITableView

- (void)fetchedData:(NSData *)responseData { 
    //parse out the json data 
    NSError* error; 
    NSDictionary* json = [NSJSONSerialization 
          JSONObjectWithData:responseData 

          options:kNilOptions 
          error:&error]; 

    NSArray* jobs = [json objectForKey:@"results"]; 

    for(NSDictionary *jobsInfo in jobs) { 

     JobInfo *jobby = [[JobInfo alloc] init]; 
     jobby.city = jobsInfo[@"city"]; 
     jobby.company = jobsInfo[@"company"]; 
     jobby.url = jobsInfo[@"url"]; 
     jobby.title = jobsInfo[@"jobtitle"]; 
     jobby.snippet = jobsInfo[@"snippet"]; 
     jobby.state = jobsInfo[@"state"]; 
     jobby.time = jobsInfo[@"date"]; 

     jobsArray = [jobsInfo objectForKey:@"results"]; 
    } 
} 

我正在循環訪問GET請求和解析的字典數組。我現在試圖填補我的UITableView用下面的代碼:

-

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

    // Return the number of rows in the section. 
    return [jobsArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSDictionary *jobsDic = [jobsArray objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:[jobsDic objectForKey:@"jobtitle"]]; 


    return cell; 
} 

而且,我宣佈這是我的.h文件:

NSArray *jobsDic; 

任何想法什麼我做錯了嗎?這是iOS 7的問題嗎?

+0

你在哪裏解析JSON到viewDidLoad? –

+0

我在tableView中選擇了一個單元格,並將它推入另一個UITableView,然後調用一個方法來解析數據。 –

+0

你有沒有檢查數組是否已經沒有進入tableView:numberOfRowsInSection: –

回答

0

似乎你在forin循環結束時重新初始化jobsarray。

你不是故意的?

NSArray* jobs = [json objectForKey:@"results"]; 
NSMutableArray *jobsTemp = [[NSMutableArray alloc] initWithCapacity:jobs.count]; 
for(NSDictionary *jobsInfo in jobs) { 

    JobInfo *jobby = [[JobInfo alloc] init]; 
    jobby.city = jobsInfo[@"city"]; 
    jobby.company = jobsInfo[@"company"]; 
    jobby.url = jobsInfo[@"url"]; 
    jobby.title = jobsInfo[@"jobtitle"]; 
    jobby.snippet = jobsInfo[@"snippet"]; 
    jobby.state = jobsInfo[@"state"]; 
    jobby.time = jobsInfo[@"date"]; 

    [jobsTemp addObject:jobby]; 
} 
self.jobsArray = jobsTemp; //set @property (nonatomic, copy) NSArray *jobsArray; in the .h 
[self.tableView reloadData]; //optional only if the data is loaded after the view 

在爲行方法進行的單元:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    JobInfo *job = self.jobsArray[indexPath.row]; 
    cell.textLabel.text = job.title; 

    return cell; 
} 

而且不要忘記:

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

更新 - 一個用戶建議編輯:

這是真的,計數不是一個NSArray屬性。但是由於Objective-C讓我們使用快捷方式表示法來調用帶點的方法,因此此代碼有效。你必須知道這個限制:如果你使用一個帶有自定義getter的count屬性的NSArray子類,這可能會產生副作用@property (nonatomic, strong, getter=myCustomCount) NSUInteger count。正如我認爲代碼可讀性對我來說是我最喜歡使用點符號的最重要的事情之一。我認爲蘋果應該實現計數作爲只讀屬性,所以我用這種方式(但這是我的觀點)。所以對於那些不同意我的人,在tableView:numberOfRowsInSection:方法中只讀return [self.jobsArray count];

+0

工程很漂亮!謝謝! –

+0

不客氣;) – Francescu

0

將jobsArray的聲明從NSArray更改爲NSMutableArray。

fetchedData的開始點添加一個初始化,如下所示。

if(!jobsArray) { 
    jobsArray = [NSMutableArray array]; 
} 
else { 
    [jobsArray removeAllObjects]; 
} 

刪除以下行。

jobsArray = [jobsInfo objectForKey:@"results"]; 

取而代之的是,在for循環結束時的初始化的對象添加到陣列。

[jobsArray addObject:jobby]; 

添加一個[tableView reloadData];在你的* - (void)fetchedData結尾:(NSData )responseData;方法的實現。

最初當你加載視圖時,tableView將被填充。在收到數據後,tableView將不知道它收到。

其他一切似乎都不錯。希望休息會正常工作。