2011-11-10 58 views
0

要填充兩個NSMutableArray s到的tableView 2個自定義欄目;填充NSMutableArrays定製部分

我有兩個NSMutableArray s的事件,我想將它們分開到現在,今天的部分。

對於第一部分:

我想從nowEvents陣列刪除事件,並將它們放到我的弗里斯特部分。

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

event.startTime是我的事件

event.endTime的開始時間是我的事件

對於第2段的結束時間: 刪除的事件,現在正在發生

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

我想知道的是numberOfRowsInSection方法,它會是什麼樣子和cellForRowAtIndexPath(她e我試過NSInteger section = [indexPath section]; if (section == 0) { } if (section == 1) {} - 但如果我現在不會發生這樣的事件,怎麼辦?)

回答

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,因爲沒有什麼可顯示的。希望這可以幫助。

+0

它不會在原始顯示,但它會顯示我的頭部分,它是具有自定義圖像,我不希望顯示的,如果我所以沒有在該節 –

+0

無論如何你 - ( CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;如果if([self.tableView numberOfRowsInSection:section] <= 0)返回0; –

+0

實際上它在我的 - (UIView的*)的tableView:(UITableView的*)的tableView viewForHeaderInSection:(NSInteger的)部分的方法;如果我在這裏把if條件,我的應用程序崩潰, –

1

根據具體情況,您可以在表格視圖中有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 ...*/ }