Iphone 在iOS 7中使用NSArray填充UITableView
在iOS 7中,为了设置UITableView单元格的字体、textLabel和颜色,许多方法已被弃用。我也很难用这些值填充视图。以下是我的代码片段:Iphone 在iOS 7中使用NSArray填充UITableView,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,在iOS 7中,为了设置UITableView单元格的字体、textLabel和颜色,许多方法已被弃用。我也很难用这些值填充视图。以下是我的代码片段: - (void)fetchedData:(NSData *)responseData { //parse out the json data NSError* error; NSDictionary* json = [NSJSONSerialization JSONObj
- (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:
-
此外,我已在我的.h文件中声明:
NSArray *jobsDic;
你知道我做错了什么吗?这是iOS 7的问题吗?似乎您在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;
}
更新-用户建议编辑:
的确,count不是NSArray的财产。但由于Objective-C允许我们使用一个快捷符号来调用带点的方法,所以这段代码是有效的。您必须知道它的局限性:如果您使用带有count属性的NSArray子类和自定义getter,这可能会产生副作用
@property(非原子,强,getter=myCustomCount)和integer count
。因为我认为代码可读性对我来说是最重要的事情之一,所以我更喜欢使用点符号。我认为苹果应该将count实现为只读属性,所以我这样使用它(但这是我的观点)。因此,对于那些不同意我的观点的人,只需阅读return[self.jobsArray count]:numberOfRowsInSection:method。将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将不知道是否收到了数据
其他一切似乎都很好。希望rest可以正常工作。您在哪里将JSON解析为viewDidLoad?我在tableView中选择了一个单元格,它将推送到另一个UITableView,然后在那里调用一个方法来解析数据。您是否检查了tableView中的数组是否为零:numberOfRowsInSection:工作得很好!非常感谢。
[jobsArray addObject:jobby];