Iphone 将NSMutableArray填充到自定义节
要将两个Iphone 将NSMutableArray填充到自定义节,iphone,uitableview,nsmutablearray,ios5,tableview,Iphone,Uitableview,Nsmutablearray,Ios5,Tableview,要将两个NSMutableArrays填充到tableView的两个自定义部分 我有两个带有事件的NSMutableArrays,我想将它们拆分为“现在”和“今天”部分 第一节: 我想从nowEvents数组中删除事件,并将它们放入我的第一部分 EventClass *event = [appDelegate.nowEvents objectAtIndex:indexPath.row]; event.startTime是我的事件的开始时间 event.endTime是我的事件的结束时间 第二节
NSMutableArray
s填充到tableView
的两个自定义部分
我有两个带有事件的NSMutableArray
s,我想将它们拆分为“现在”和“今天”部分
第一节:
我想从nowEvents数组中删除事件,并将它们放入我的第一部分
EventClass *event = [appDelegate.nowEvents objectAtIndex:indexPath.row];
event.startTime
是我的事件的开始时间
event.endTime
是我的事件的结束时间
第二节:
删除当前正在发生的事件
EventClass *event = [appDelegate.todayEvents objectAtIndex:indexPath.row];
我想知道的是
numberOfRowsInSection
方法,它是什么样子,以及cellForRowAtIndexPath
(在这里我尝试了NSInteger节=[indexPath节];if(节==0){}if(节==1){}
,但是如果我现在没有发生的事件呢?)根据具体情况,表视图中可以有1或2个部分,然后在numberOfRowsInSection中检查现在是否有任何事件(如果没有,则删除该部分)。所以你的假设应该是
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 ...*/ }
像这样的东西应该有用
#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,因为没有显示任何内容。希望这有帮助。它不会显示在raw中,但会显示我的标题部分,这是一个自定义图像,如果该部分中没有任何事件,我不想显示,因此在您的-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)部分;如果([self.tableView numberOfRowsInSection:section]实际上在my-(UIView*)tableView:(UITableView*)tableView for HeaderInsection:(NSInteger)中,则只需使用以下内容section方法;如果我在此处放置if条件,我的应用程序将崩溃如果您将高度设置为0,则不应为该节调用ViewForHeaderSection,您可以通过打印ViewForHeaderSection请求的节来测试这一点,方法是使用NSLog输出传入的节。