[英]Populate NSMutableArrays to custom sections
Want to populate two NSMutableArray
s to 2 Custom Sections of tableView
; 想要将两个
NSMutableArray
填充到tableView
2个自定义部分;
I have two NSMutableArray
s with events and I want to split them to now and today sections. 我有两个带事件的
NSMutableArray
,我想将它们分成现在和今天的部分。
For the first section: 对于第一部分:
I want to remove events from nowEvents Array and place them into my frist section. 我想从nowEvents数组中删除事件并将它们放入我的frist部分。
EventClass *event = [appDelegate.nowEvents objectAtIndex:indexPath.row];
event.startTime
is the start time of my event event.startTime
是我的活动的开始时间
event.endTime
is the end time of my event event.endTime
是我活动的结束时间
For 2nd section: Remove the events that are happening now 对于第2部分:删除现在发生的事件
EventClass *event = [appDelegate.todayEvents objectAtIndex:indexPath.row];
What I'd like to know is numberOfRowsInSection
method, how it would look like and cellForRowAtIndexPath
(here I've tried NSInteger section = [indexPath section]; if (section == 0) { } if (section == 1) {}
- but what about if I won't have an event that is happening now ?) 我想知道的是
numberOfRowsInSection
方法,它看起来是怎么样的和cellForRowAtIndexPath
(这里我试过NSInteger section = [indexPath section]; if (section == 0) { } if (section == 1) {}
- 但是,如果我现在不会发生正在发生的事件怎么办?)
You can have 1 or 2 sections in your table view depending on the case, then in your numberOfRowsInSection you check if there are any events now (if they aren't then you drop that section). 根据具体情况,您可以在表格视图中包含1个或2个部分,然后在numberOfRowsInSection中检查现在是否有任何事件(如果不是,则删除该部分)。 So Your ifs would be something like
所以你的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 ...*/ }
Something like this should work 这样的事情应该有效
#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;
}
If you don't have an event happening now then your nowEvent array will be empty so in the numberOfRowsInSection will return 0 and therefore the cellForRowAtIndexPath won't be called as there is nothing to display. 如果您现在没有发生事件,那么您的nowEvent数组将为空,因此numberOfRowsInSection将返回0,因此不会调用cellForRowAtIndexPath,因为没有任何内容可以显示。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.