如何根据webservice的响应在UITableView中形成动态行和节

时间:2014-06-22 18:24:03

标签: ios objective-c uitableview

我能够从存储在数组中的Web服务获取响应。这个数组必须分成UITableview的部分和行。来自具有相同ComputedDate的webservice公告的响应应该是部分标题和公告标题是基于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;
}

2 个答案:

答案 0 :(得分:0)

当前代码的问题对我来说并不完全清楚,因为我不知道应该发生什么,反之亦然。所以我还在猜测。但我发现你没有实现viewForHeaderInSection,这是我用来实现自定义标头的方法。另外我认为你需要将heightForHeaderInSection设置为不同于0的东西(显然)。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // Simple on the fly generated UIView, you could also return your own subclass of UIView
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:12]];
    label.text = [arrHeaders objectAtIndex:section];

    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    // Should be same as UIView that you return in previous function
    return 18.0f;
}

答案 1 :(得分:0)

好的,你想要嵌套你的数据。我建议使用自定义类来存储您喜欢的数据:

·H:

@interface AnnouncementGroup : NSObject

@property NSString *computedDate;
@property NSMutableArray* announcements;

- (id)initWithComputedDate:(NSString *)computedDate;

@end

的.m:

#import "AnnouncementGroup.h"
@implementation AnnouncementGroup

- (id)initWithComputedDate:(NSString *)computedDate;
{
    self = [super init];

    self.announcements = [[NSMutableArray alloc] init];
    self.computedDate = computedDate;

    return self;
}

@end 

然后我们需要遍历您的数据并对其进行排序:

// Where you want to start processing
NSMutableArray *announcementGroups = [[NSMutableArray alloc] init];

for (NSDictionary *announcementResult in arrResult)
{
    AnnouncementGroup *group = [ThisClassName newOrExistingAnnouncementGroupForComputedDate:computedDate searchInList:announcementGroups];

    Announcement *at = [[Announcement alloc] init];
    at.announcementTitle = [dictResSub valueForKey:@"AnnouncementTitle"];
    at.announcementPostedTime = [dictResSub valueForKey:@"AnnouncementPostedTime"];
    at.computedDate = [dictResSub valueForKey:@"ComputedDate"];

    [group.announcements addObject:at];
}

// Static function on same object, will return a new group if the computedDate is not in the list
+ (AnnouncementGroup *)newOrExistingAnnouncementGroupForComputedDate:(NSString *)computedDate searchInList:(NSArray *)announcementGroups;
{
    for (AnnouncementGroup *group in announcementGroups)
    {
        if ([group.computedDate equals:computedDate])
        {
            return group;
        }
    }

    AnnouncementGroup newGroup = [[AnnouncementGroup alloc] initWithComputedDate:computedDate];
    [announcementGroups addObject:newGroup];

    return newGroup;
}

如您所见,此代码将为给定日期创建一个新组,并将其添加到包含组的列表中,除非该日期已用于组,否则它将返回现有组。在这两种情况下,公告都将添加到该AnnouncementGroup对象中。现在你有一个正确嵌套的数据模型。

现在你的UITableView实现中的一些函数:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return announcementGroups.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    AnnouncementGroup group = (AnnouncementGroup *) [announcementGroups objectAtIndex:section];
    return group.computedDate;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    AnnouncementGroup group = (AnnouncementGroup *) [announcementGroups objectAtIndex:section];
    return group.announcements.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"];
    }

    AnnouncementGroup *group = [announcementGroups objectAtIndex:indexPath.section];
    Announcement *announcement = [group objectAtIndex:indexPath.row];

    cell.textLabel.text= [NSString stringWithFormat:@"%@",announcement.announcementTitle];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor=[UIColor clearColor];
    return cell;
}

如您所见,这需要更多的演员,但我认为只会让您的代码更干净,更易于维护。