我返回了从大多数视图到最少视图排列的 json 数据,但它在表格视图中的排列完全错误......这就是它的样子
表格截图
这是返回的数组
{
author = edmiester777;
description = "test post #3";
id = 3;
imageURL = "https://lh5.googleusercontent.com/-nmbZ4yKmKyQ/Tz6t5kpwAuI/AAAAAAAAADA/CO0s0VhvPDM/w800-h800/potion.jpg";
pubDate = "2013-07-01 12:49:01";
title = "Post #3";
views = 64774;
},
{
author = edmiester777;
description = "@*$(DS(XC#*$&@(())));";
id = 1;
imageURL = "http://0.tqn.com/d/sbinformation/1/0/D/A/twitter_newbird_boxed_whiteonblue.png";
pubDate = "2013-07-01 12:21:01";
title = "First Post Ever!";
views = 1035;
},
{
author = edmiester777;
description = "this post is testing the UIImage loading capability of the custom table view";
id = 2;
imageURL = "http://greatergood.berkeley.edu/images/uploads/Thnx4-logo-small.png";
pubDate = "2013-07-01 12:46:28";
title = "Second Post Ever!";
views = 645;
}
显示数据时会出现什么问题?
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Regular_Cell *cell;
// Configure the cell...
if(indexPath.section == 0)
{
if(indexPath.row < NUMBER_OF_DYNAMIC_ROWS_RECENT)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"default"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"default"];
}
[cell.title_label setText:[self.recent_array_titles objectAtIndex:[indexPath row]]];
[cell.views_label setText:[self.recent_array_views objectAtIndex:[indexPath row]]];
[cell.author_label setText:[self.recent_array_authors objectAtIndex:[indexPath row]]];
[cell setPoll_idFromString:[self.recent_array_poll_id objectAtIndex:[indexPath row]]];
//options for image view
cell.desc_image.contentMode = UIViewContentModeScaleAspectFill;
NSString *imageURL = [self.recent_array_poll_images objectAtIndex:[indexPath row]];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
[cell.desc_image setImage:[UIImage imageWithData:imageData]];
}
else if(indexPath.row == NUMBER_OF_DYNAMIC_ROWS_RECENT)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"See More"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"See More"];
}
}
else
{
cell = nil;
}
}
if(indexPath.section == 1)
{
if(indexPath.row < NUMBER_OF_DYNAMIC_ROWS_TOP)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"default"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"default"];
}
[cell.title_label setText:[self.recent_array_titles objectAtIndex:[indexPath row]]];
[cell.views_label setText:[self.recent_array_views objectAtIndex:[indexPath row]]];
[cell.author_label setText:[self.recent_array_authors objectAtIndex:[indexPath row]]];
[cell setPoll_idFromString:[self.recent_array_poll_id objectAtIndex:[indexPath row]]];
//options for image view
cell.desc_image.contentMode = UIViewContentModeScaleAspectFill;
NSString *imageURL = [self.recent_array_poll_images objectAtIndex:[indexPath row]];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
[cell.desc_image setImage:[UIImage imageWithData:imageData]];
}
else if(indexPath.row == NUMBER_OF_DYNAMIC_ROWS_TOP)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"See More"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"See More"];
}
}
else
{
cell = nil;
}
}
return cell;
}
这就是我获取每个单独数组的方式
-(void)addRecentCells
{
NSArray *recArray = [Global_Vars get_json_results_for_recent];
self.recent_array_titles = [[NSMutableArray alloc]init];
self.recent_array_authors = [[NSMutableArray alloc]init];
self.recent_array_views = [[NSMutableArray alloc]init];
self.recent_array_poll_id = [[NSMutableArray alloc]init];
self.recent_array_poll_images = [[NSMutableArray alloc]init];
for(NSDictionary *poll in recArray)
{
[self.recent_array_titles addObject:poll[@"title"]];
[self.recent_array_authors addObject:[NSString stringWithFormat:@"Author: %@",poll[@"author"]]];
[self.recent_array_views addObject:[NSString stringWithFormat:@"Views : %@",poll[@"views"]]];
[self.recent_array_poll_id addObject:[NSString stringWithFormat:@"%@",poll[@"id"]]];
[self.recent_array_poll_images addObject:[NSString stringWithFormat:@"%@",poll[@"imageURL"]]];
NUMBER_OF_DYNAMIC_ROWS_RECENT = self.recent_array_titles.count;
}
[self.tableView reloadData];
}
-(void)addTopViewedCells
{
NSArray *topArray = [Global_Vars get_json_results_for_top_viewed];
self.top_views_array_titles = [[NSMutableArray alloc]init];
self.top_views_array_authors = [[NSMutableArray alloc]init];
self.top_views_array_views = [[NSMutableArray alloc]init];
self.top_views_array_poll_id = [[NSMutableArray alloc]init];
self.top_views_array_poll_images = [[NSMutableArray alloc]init];
for(int i = 0; i < topArray.count; i++)
{
NSDictionary *poll = [topArray objectAtIndex:i];
[self.top_views_array_titles addObject:poll[@"title"]];
[self.top_views_array_authors addObject:[NSString stringWithFormat:@"Author: %@",poll[@"author"]]];
[self.top_views_array_views addObject:[NSString stringWithFormat:@"Views : %@",poll[@"views"]]];
[self.top_views_array_poll_id addObject:[NSString stringWithFormat:@"%@",poll[@"id"]]];
[self.top_views_array_poll_images addObject:[NSString stringWithFormat:@"%@",poll[@"imageURL"]]];
NUMBER_OF_DYNAMIC_ROWS_TOP = self.top_views_array_titles.count;
}
[self.tableView reloadData];
}