将 NSMutableArrays 填充到自定义部分

发布于 2024-12-14 09:47:18 字数 724 浏览 6 评论 0原文

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

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

对于第一部分:

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

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

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

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

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

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

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

Want to populate two NSMutableArrays to 2 Custom Sections of tableView;

I have two NSMutableArrays with events and I want to split them to now and today sections.

For the first section:

I want to remove events from nowEvents Array and place them into my frist section.

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

event.startTime is the start time of my event

event.endTime is the end time of my event

For 2nd section:
Remove the events that are happening now

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 ?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2024-12-21 09:47:18

这样的事情应该可以工作

#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,因为没有任何内容可显示。希望这有帮助。

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. Hope this helps.

饮惑 2024-12-21 09:47:18

根据情况,您的表视图中可以有 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 ...*/ }

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). So Your ifs would be something like

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 ...*/ }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文