【发布时间】:2014-06-23 05:25:17
【问题描述】:
我能够从存储在数组中的 Web 服务获取响应。该数组必须分隔为 UITableview 的部分和行。作为响应,具有相同 ComputedDate 的 Web 服务公告应作为部分标题和公告标题具有基于computedDate成为该部分的行。我能够形成haeders,但我的问题是所有行对于所有部分都是通用的。 UITableView 中的 o/p 应该是:
June 21,2014
Performance and Load
June 20,2014
My teest
Sample
June 19,2014
All samp
June 18,2014
qwerty
响应格式为:
{ "ResultSet1": [
{
"AnnouncementId": 3,
"AnnouncementTitle": "Performance and Load",
"AnnouncementPostedTime": "21 Jun,2014,11:07 AM",
"ComputedDate": "Jun 21,2014",
},{
"AnnouncementId": 22,
"AnnouncementTitle": "My teest",
"AnnouncementPostedTime": "20 Jun,2014,10:11 AM",
"ComputedDate": "Jun 20,2014",
}, {
"AnnouncementId": 21,
"AnnouncementTitle": "Sample",
"AnnouncementPostedTime": "20 Jun,2014,10:11 AM",
"ComputedDate": "Jun 20,2014",
},
{
"AnnouncementId": 20,
"AnnouncementTitle": "All samp",
"AnnouncementPostedTime": "19 Jun,2014,10:11 AM",
"ComputedDate": "Jun 19,2014",
}, {
"AnnouncementId": 19,
"AnnouncementTitle": "qwerty",
"AnnouncementPostedTime": "18 Jun,2014,10:10 AM",
"ComputedDate": "Jun 18,2014",
}
]
}
我的代码如下:
arrResult=[dict valueForKey:@"ResultSet1"];
listOfObjects = [NSMutableArray array];
for (NSDictionary *dictResSub in arrResult)
{
Announcements *at = [[Announcements alloc] init];
at.announcementTitle = [dictResSub valueForKey:@"AnnouncementTitle"];
at.announcementPostedTime = [dictResSub valueForKey:@"AnnouncementPostedTime"];
at.computedDate=[dictResSub valueForKey:@"ComputedDate"];
[listOfObjects addObject:at];
}
int i;
for (i=0 ;i< [listOfObjects count];i++){
Announcements *atAnnouncement1p1;
NSString *str1p1;
Announcements *atAnnouncement1=[listOfObjects objectAtIndex:i];
NSString *str1=[NSString stringWithFormat:@"%@",atAnnouncement1.computedDate];
if (i==0) {
[arrHeaders addObject:str1];
[arrFirstObj addObject:atAnnouncement1];
}
if (i<[listOfObjects count]-1) {
atAnnouncement1p1=[listOfObjects objectAtIndex:i+1];
str1p1=[NSString stringWithFormat:@"%@",atAnnouncement1p1.computedDate];
if ([str1 isEqualToString:str1p1]) {
NSLog(@"Already there");
[arrFirstObj addObject:atAnnouncement1p1];
}
else{
[arrHeaders addObject:str1p1];
}
}
}
}
// [tableVwCategories reloadData];
tableVwCategories.frame=CGRectMake(0, 70, 320, 300);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [arrHeaders count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [arrHeaders objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrFirstObj count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}
Announcements *at = [arrFirstObj objectAtIndex:indexPath.row];
Cell.textLabel.text= [NSString stringWithFormat:@"%@",at.announcementTitle];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
return cell;
}
【问题讨论】:
-
如何将数据分成UITableview的行和部分
标签: ios objective-c uitableview