Łączenie zawartości statycznej i prototypowej w widoku tabeli

Czy istnieje sposób na połączenie statycznych komórek tableview (zawartość statyczna) z dynamicznymi komórkami tableview (zawartość prototypu) za pomocą storyboard?

Author: BoltClock, 2012-02-17

8 answers

Sugeruję, abyś traktował swój stół jako dynamiczny, ale uwzględniaj komórki, które zawsze chcesz na górze. W Storyboardzie umieść UITableViewController i użyj dynamicznej tabeli. Dodaj do tabeli tyle prototypów UITableViewCell, ile potrzebujesz. Powiedzmy, po jednej dla komórek statycznych, a po jednej dla komórek zmiennych.

W klasie UITableViewDataSource:

#define NUMBER_OF_STATIC_CELLS  3

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dynamicModel count] + NUMBER_OF_STATIC_CELLS;
}

I wtedy

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row < NUMBER_OF_STATIC_CELLS) {
        // dequeue and configure my static cell for indexPath.row
        NSString *cellIdentifier = ... // id for one of my static cells
    } else {
        // normal dynamic logic here
        NSString *cellIdentifier = @"DynamicCellID"
        // dequeue and configure for [self.myDynamicModel objectAtIndex:indexPath.row]
    }
}
 55
Author: danh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-25 17:32:48

[1]} miałem problem, chociaż był to niewielki wariant tego. Właściwie chciałem mieszać komórki dynamiczne i statyczne, ale w różnych grupach. Oznacza to, że grupa 1 miałaby tylko komórki statyczne, a grupa 2 miałaby komórki dynamiczne.

Udało mi się to osiągnąć poprzez twarde kodowanie statycznych wartości komórek (na podstawie ich prototypowych identyfikatorów komórek). Sekcje dynamiczne będą miały normalną, dynamicznie wypełnioną zawartość. Oto przykładowy kod na wypadek, gdyby ktoś inny miał ten sam problem:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{    
    if (section == 1){
        return @"Dynamic Cells";
    }
    if (section == 0){
        return @"Static Cells";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1; //However many static cells you want
    } else {
        return [_yourArray count];
    }
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.section == 0) {
        NSString *cellIdentifier = @"staticCellType";   
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        cell.textLabel.text = @"some static content";        
        return cell;

    } else if (indexPath.section == 1){

        NSString *cellIdentifier = @"dynamicCellType";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        cell.textLabel.text = [_yourArray objectAtIndex:indexPath.row];
        return cell;

    } 
    return nil;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
 12
Author: AbuZubair,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-01 00:40:55

Ponieważ nikt nie dostarczył prawdziwej odpowiedzi na problem (używając zarówno statycznych, jak i prototypowych komórek w tym samym widoku tabeli), pomyślałem, że się odezwę. Da się zrobić!

Twórz statyczne komórki według własnego uznania. W przypadku sekcji, które wymagają komórki dynamicznej, jeśli nie używasz standardowego typu UITableViewCell, musisz utworzyć niestandardową w oddzielnej stalówce, w przeciwnym razie możesz użyć standardowych. Następnie wdrożyć następujące delegatów. Zasadniczo dla każdego z tych delegaci, za statyczne rzeczy, które chcemy nazwać super, za dynamiczne, zwracamy nasze wartości.

Po pierwsze, jeśli chcesz selektywnie pokazać swoją dynamiczną sekcję, będziesz chciał zaimplementować numerofsectionsintableview (w przeciwnym razie możesz pominąć ten delegat):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    int staticSections = 1;
    int dynamicSections = 1;
    if (SOME_BOOLEAN) {
        return staticSections + dynamicSections;
    } else {
        return staticSections;
    }
}

Następnie należy zaimplementować numerofrowsinsection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return A_COUNT;
    } else {
        return [super tableView:tableView numberOfRowsInSection:section];
    }
}

Następnie musisz zaimplementować heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 44.0f;
    } else {
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
}

Then indentationLevelForRowAtIndexPath:

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 1; // or manually set in IB (Storyboard)
    } else {
        return [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath]; // or 0
    }
}

Wreszcie, cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        SomeObject *obj = self.someArray[indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DynamicCell" forIndexPath:indexPath];
        cell.textLabel.text = obj.textValue;
        return cell;
    } else {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}
 5
Author: Sean,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-24 15:27:43

Zawsze możesz sprawić, że twój widok tabel będzie podobny do statycznej tabeli, ale zdefiniuj go w kodzie. Ustaw sekcje, ilość lub wiersze na sekcję, nagłówki itp. poprzez metody delegowania.

 3
Author: Michael,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-16 00:27:17

Niestety, nie jest to możliwe, ponieważ statyczne widoki tabel muszą znajdować się w kontrolerze UITableViewController, który pozwala tylko na jeden widok tableview.

To, co musisz zrobić, to zrobić trzy bardziej dynamiczne UITableviewCell ' s i załadować je indywidualnie dla pierwszych trzech wierszy, w których chcesz zawartość statyczną.

Jeśli nie jesteś pewien, jak to zrobić, daj mi znać, a znajdę jakiś kod.

 3
Author: lnafziger,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-17 22:36:17

Nie możesz mieć jednego widoku statycznego, a drugiego dynamicznego w tym samym kontrolerze widoku, więc musisz sprawić, by oba widoki były dynamiczne. W pierwszym widoku tableview skonfigurujesz komórki w kodzie podczas inicjowania kontrolera widoku nigdy ich nie Aktualizuj.

  1. Dodaj Kontroler UIViewController do swojego storyboardu.
  2. Dodaj dwa widoki tabeli (nie TableViewControllers) do kontrolera UIView.
  3. Wybierz każdy widok tableView i skonfiguruj oba dla komórek dynamicznych.
  4. Zbuduj i dołącz kontroler widoku. 2 tableview w pojedynczym widoku wyjaśnia ten krok.

Jako inną opcję możesz uzyskać podobny wygląd, osadzając dynamiczny widok tabeli w części widoku podobnego do łącza w kroku 4, a następnie zrobić to, co chcesz w pozostałej części widoku, aby skonfigurować to, co planujesz zrobić ze statycznymi komórkami za pomocą przewijania, etykiet i przycisków.

 2
Author: T.J.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:16:36

Możesz również utworzyć przyciski (po jednym dla każdej statycznej komórki), które są stylizowane jak twoje komórki i umieścić je w widoku tableHeaderView lub tableFooterView w widoku UITableView; te przyciski są po prostu widokami.

Musisz dodać logikę dokonywania wyborów na przyciskach i komórkach, aby zachować zwykły wygląd.

Oczywiście zakłada się, że chcesz wstawić statyczne komórki do widoku tabeli u góry lub u dołu tabeli.

 0
Author: BeckProduct,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-17 12:54:18

Jednym ze sposobów na posiadanie dynamicznej zawartości w statycznym widoku tabeli jest klonowanie komórek, w których potrzebne są dodatkowe wiersze.

Dla dynamicznej sekcji mojego widoku tabeli, układam jedną lub więcej komórek w Interface Builder. W czasie wykonywania mogę je sklonować, archiwizując za pomocą Nscodera, a następnie unarchiving.

To działa, ale niekoniecznie jest ładniejsze niż zaczynanie od dynamicznego prototypowego widoku tabeli i tworzenie statycznych wierszy.

Nie powiodło się ze standardowymi komórkami widoku tabeli. Leniwiec utworzone etykiety tekstowe nie są prawidłowo ułożone. Dlatego użyłem podklas UITableViewCell, gdzie zajmuję się archiwizacją i unarchiwaniem podwidów.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == kContactsSection) {
        NSArray     *contacts   = self.contacts;
        Contact     *contact    = [contacts objectAtIndex:indexPath.row];
        NSString    *name       = contact.name;
        NSString    *role       = contact.role;

        if ([role length] == 0) {
            NNContactDefaultTableViewCell *cell = (id)[tableView dequeueReusableCellWithIdentifier : @"contactDefault"];

            if (cell == nil) {
                NNContactDefaultTableViewCell   *template   = (id)[super tableView : tableView
                                                         cellForRowAtIndexPath :[NSIndexPath indexPathForRow:0 inSection:kContactsSection]];

                NSData                          *data       = [NSKeyedArchiver archivedDataWithRootObject:template];

                cell = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            }

            cell.contactTextLabel.text = name;

            return cell;
        }
        else {
            NNContactDetailTableViewCell *cell = (id)[tableView dequeueReusableCellWithIdentifier : @"contactDetail"];

            if (cell == nil) {
                NNContactDetailTableViewCell    *template   = (id)[super tableView : tableView
                                                        cellForRowAtIndexPath :[NSIndexPath indexPathForRow:1 inSection:kContactsSection]];

                NSData                          *data       = [NSKeyedArchiver archivedDataWithRootObject:template];

                cell = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            }

            cell.contactTextLabel.text          = name;
            cell.contactDetailTextLabel.text    = role;

            return cell;
        }
    }

    return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}

W powyższym przykładzie mam dwa typy komórek. Oba przedstawione w Interface Builder jako część statycznego widoku tabeli.

Aby uzyskać dynamiczną zawartość w jednej sekcji, muszę również nadpisać następujące metody:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == kContactsSection) {
        NSArray     *contacts       = self.contacts;
        NSUInteger  contactCount    = [contacts count];

        return contactCount;
    }

    return [super tableView:tableView numberOfRowsInSection:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger   section = indexPath.section;
    NSInteger   row     = indexPath.row;

    if (section == kContactsSection) {
        return [super tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:kContactsSection]];
    }

    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger   section     = indexPath.section;

    if (section == kContactsSection) {
        CGFloat indentation = [super tableView:tableView indentationLevelForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:kContactsSection]];

        return indentation;
    }

    CGFloat     indentation = [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath];

    return indentation;
}
 0
Author: Pierre Bernard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-04 19:22:40