【问题标题】:How to form dynamic rows and sections in UITableView based on response from webservice如何根据来自 web 服务的响应在 UITableView 中形成动态行和部分
【发布时间】: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


【解决方案1】:

当前代码的问题对我来说并不完全清楚,因为我不知道什么不会发生什么应该发生,反之亦然。所以我还是有点猜测。但我看到你没有实现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;
}

【讨论】:

  • 输出应该是上述格式。我在将获得的数据分成部分和行时遇到问题
  • 好的,我将使用另一个答案来解决这个问题。
【解决方案2】:

好的,你想嵌套你的数据。我建议使用自定义类来存储您的数据,如下所示:

.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;
}

如您所见,这需要更多的转换,但我认为这只会使您的代码更干净、更易于维护。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多