【问题标题】:Populate NSMutableArrays to custom sections将 NSMutableArrays 填充到自定义部分
【发布时间】:2011-11-10 08:43:20
【问题描述】:

想要将两个 NSMutableArrays 填充到 tableView 的 2 个自定义部分;

我有两个带有事件的NSMutableArrays,我想将它们分成现在和今天部分。

对于第一部分:

我想从 nowEvents 数组中删除事件并将它们放入我的第一个部分。

EventClass *event = [appDelegate.nowEvents objectAtIndex:indexPath.row];

event.startTime是我的活动开始时间

event.endTime是我活动的结束时间

对于第二部分: 移除正在发生的事件

EventClass *event = [appDelegate.todayEvents objectAtIndex:indexPath.row];

我想知道的是numberOfRowsInSection 方法,它的外观和cellForRowAtIndexPath(这里我尝试过NSInteger section = [indexPath section]; if (section == 0) { } if (section == 1) {} - 但是如果我现在没有正在发生的事件呢? ?)

【问题讨论】:

    标签: iphone uitableview nsmutablearray ios5 tableview


    【解决方案1】:

    这样的东西应该可以工作

    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    #warning Incomplete method implementation.
        // Return the number of rows in the section.
        switch (section) {
            case 0:
                return [appDelegate.nowEvents count];
            case 1:
                return [appDelegate.todayEvents count];    
            default:
                return 0;
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
    
        switch (indexPath.section) {
            case 0:
                EventClass *nowEvent = [appDelegate.nowEvents objectAtIndex:indexPath.row];
                //set up cell to display this event
                break;
            case 1:
                EventClass *todayEvent = [appDelegate.todayEvents objectAtIndex:indexPath.row];
                //set up cell to display this event
                break;    
            default:
                break;
        }
    
        // Configure the cell...
    
        return cell;
    }
    

    如果您现在没有发生事件,那么您的 nowEvent 数组将为空,因此 numberOfRowsInSection 将返回 0,因此不会调用 cellForRowAtIndexPath,因为没有可显示的内容。希望这会有所帮助。

    【讨论】:

    • 它不会以原始格式显示,但会显示我的标题部分,该部分具有自定义图像,如果该部分中没有任何事件,我不想显示该部分
    • 所以在你的 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; if([self.tableView numberOfRowsInSection:section]
    • 其实它在我的 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 方法中;如果我把 if 条件放在这里,我的应用程序就会崩溃
    • 如果将高度设置为 0,则不应为该部分调用 viewForHeaderInSection,您可以通过打印出 viewForHeaderInSection 请求的部分来测试这一点,方法是使用 NSLog 输出传入的部分。
    【解决方案2】:

    根据情况,您可以在表格视图中包含 1 或 2 个部分,然后在 numberOfRowsInSection 中检查现在是否有任何事件(如果没有,则删除该部分)。所以你的 ifs 会是这样的

    BOOL eventsNow = YES/NO;
    if (section == 0 && eventsNow) { } 
    if (section == 0 && !eventsNow) { } 
    if (section == 1 && eventsNow) { }
    if (section == 1 && !eventsNow) { /* This case shouldn't happen so assert or throw ...*/ }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 2015-03-28
      • 2010-10-07
      • 2021-11-21
      相关资源
      最近更新 更多