【问题标题】:Sectioning the NSMutableArray of dictionaries分割字典的 NSMutableArray
【发布时间】:2015-07-13 14:59:48
【问题描述】:

我有如下的字典数组,我需要根据“create_time”作为 UITableView 的section-title 对下面的数组进行分段。并相应地加载到 tableview 中。请帮助如何使格式化数组具有节。

 ({
        content = hellow;
        "create_time" = 1436797644;
        "from_id" = 290;
        "from_name" = waterfall;
        "group_id" = "<null>";
        id = 1;
        "send_time" = 1436797644;
        "session_id" = 1436797620;
        "state_id" = 0;
        "to_id" = 289;
        "to_name" = prof2;
        "type_id" = 1;
    } );

这是关于我所做的代码的一些小东西:

arr_CompleteChat=[[DataBaseController getInstance] readChat:userID withFromId:friendID withLimit:20];

 SqlStatementt =[NSString stringWithFormat:@"SELECT * FROM MessageList where (from_id=%ld and to_id=%ld) OR  (from_id=%ld and to_id=%ld) order by create_time DESC LIMIT %d",(long)[to_id integerValue],(long)[from_id integerValue],mode,(long)[from_id integerValue],(long)[to_id integerValue],limit];

关于表格视图的加载:

         -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
         {
          return [arr_CompleteChat count];
         }
         -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
        {
           return 1;
         }

      -(UITableViewCell *)tableView:(UITableView *)tableView      
cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if([[[arr_CompleteChat objectAtIndex:indexPath.row]valueForKey:@"from_id"] isEqualToString:[[NSUserDefaults standardUserDefaults] valueForKey:@"MY_ID"]])
{
    static NSString *CellIdentifier = @"CellIdentifier";
    SendMsgBubbleCell *temp=(SendMsgBubbleCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (temp == nil)
    {
        temp = [[SendMsgBubbleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SendMsgBubbleCell" owner:temp options:nil];
        [temp loaditemwithMessageArray:[arr_CompleteChat objectAtIndex:indexPath.row]];

        temp=[topLevelObjects objectAtIndex:0];
    }


    return temp;
}
else
{
    static NSString *CellIdentifier = @"CellIdentifier";
    ReciveMsgBubbleCell *temp=(ReciveMsgBubbleCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (temp == nil)
    {
        temp = [[ReciveMsgBubbleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ReciveMsgBubbleCell" owner:temp options:nil];
        [temp loaditemwithMessageArray:[arr_CompleteChat objectAtIndex:indexPath.row]  ];
        temp=[topLevelObjects objectAtIndex:0];
    }
    return temp;
    }
    }

现在我希望在 UItableview 的部分中转换整个 arr_completechat。我猜它的代码 sn-p 足以理解。

【问题讨论】:

  • 您最好将您的相关代码提供给我们,看看您做了什么,以便我们更轻松地提供帮助。

标签: ios objective-c iphone uitableview


【解决方案1】:

遍历数组并获取键 create_time 的值。如果表视图中已经存在具有该值的部分,则只需向其添加一个单元格。否则,您首先必须创建一个新部分,然后添加单元格。

这几乎是伪代码。一旦你知道了正确的使用方法,就很容易实现你想要的。

【讨论】:

  • 感谢您的回复。请看代码中的更新。
猜你喜欢
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 2012-11-02
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
相关资源
最近更新 更多