Заполните NSMutableArrays в пользовательские разделы - PullRequest
0 голосов
/ 10 ноября 2011

Хотите заполнить два NSMutableArray s до 2 пользовательских разделов tableView;

У меня есть два NSMutableArray с событиями, и я хочу разделить их на секции «сейчас» и «сегодня».

Для первого раздела:

Я хочу удалить события из массива nowEvents и поместить их в мой первый раздел.

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

event.startTime время начала моего мероприятия

event.endTime время окончания моего мероприятия

Для 2-го раздела: Удалить события, которые происходят сейчас

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

То, что я хотел бы знать, это метод numberOfRowsInSection, как он будет выглядеть и cellForRowAtIndexPath (здесь я попробовал NSInteger section = [indexPath section]; if (section == 0) { } if (section == 1) {} - но что если у меня не будет события, которое происходит сейчас? ?)

Ответы [ 2 ]

1 голос
/ 10 ноября 2011

Примерно так должно работать

#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 не будет вызыватьсятак как нечего отображать.Надеюсь, это поможет.

1 голос
/ 10 ноября 2011

Вы можете иметь 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 ...*/ }
...