Domanda

Ho un Tableview raggruppato che viene popolato con dati XML in una sezione. Quello che vorrei fare è creare un'altra sezione prima dei dati guidato uno, e applicare un'azione ad esso.

Esempio:

L'utente è presentato con un pulsante che dice "utilizzare la posizione corrente" (sezione creata manualmente) e al di sotto che è un elenco di paesi che l'utente può alternativamente scegliere da scegliere (sezione Dati guidato)

Utilizzare il menu impostazioni come guida. Ci sono alcune opzioni che sono una singola riga in una sezione, in modo da apparire come un pulsante ...

Se questo non ha senso, cercherò di spiegarlo meglio.


Così ho queste due righe di codice evidenti ... abbastanza semplice

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}    
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [countrysData count];
    }

Quello che vorrei è avere numberOfSectionsInTableView ritorno 2 e avere il primo "Sezione" dire "Clicca per utilizzare la posizione corrente", che sarebbe poi spingere in vista una mappa, e la seconda sezione visualizzare l'elenco dei paesi momento ho di lavoro.

È stato utile?

Soluzione

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){
        return 1;
    }else{
        return [countrysData count];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}   

allora si dovrebbe scegliere cosa fare nella vostra didSelectRowAtIndexPath: il metodo a causa della indexPath.section. oh, e si dovrebbe verificare indexPath.section nella vostra cellForRowAtIndexPath: metodo.

Altri suggerimenti

Hai solo bisogno di aggiornare tutte le implementazioni dei protocolli UITableViewDataSource e UITableViewDelegate per tenere conto in modo appropriato per la nuova sezione e la sua fila (s).

Ad esempio, ecco come aggiornare numberOfSectionsInTableView: , tableView: numberOfRowsInSection: , e tableView: cellForRowAtIndexPath: , ma si vorranno aggiornare almeno t ableView: didSelectRowAtIndexPath: così:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // calculate the number of sections of non-data (might just be 1)    
    // calculate the number of sections for the data (you were already doing this, might just be 1)
    // return the sum
    return 1 + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger rowCount = 0;
    switch (section) {
    case 0:
        // non-data section has 1 row/cell
        rowCount = 1;
        break;
    case 1:
        // data section uses an array
        rowCount = [dataArray count];
        break;
    }
    return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *nonDataCellID = @"NonDataCell";
    static NSString *dataCellID = @"DataCell";
    UITableViewCell *cell;

    int section = [indexPath indexAtPosition:0];
    int row = [indexPath indexAtPosition:1];

    switch (section) {
        case 0:
            // or you can just use standard cells here
            cell = [tableView dequeueReusableCellWithIdentifier:nonDataCellID];
            if (cell == nil) {
                [[NSBundle mainBundle] loadNibNamed:@"NonDataCell" owner:self options:NULL];
                cell = nonDataCell; // nonDataCell is an IBOutlet to this custom cell
            }

            // configure non-data cell here (use tags)
            UILabel *someLabel = (UILabel*)[cell viewWithTag:1];
            someLabel.text = @"Non-data cell";
            break;

        case 1:
            // or you can just use standard cells here
            cell = [tableView dequeueReusableCellWithIdentifier:dataCellID];
            if (cell == nil) {
                [[NSBundle mainBundle] loadNibNamed:@"dataCell" owner:self options:NULL];
                cell = dataCell; // dataCell is an IBOutlet to this custom cell
            }

            // configure data call here (using "row")
            UILabel *someDataLabel = (UILabel*)[cell viewWithTag:1];
            someDataLabel.text = [[dataArray objectAtIndex:row] valueForKey:@"name"];
            break;
       }

    return cell;
}

Sono abbastanza sicuro che è possibile modificare il ritorno del metodo di UITableViewDataSource 'numberOfSectionsInTableView:' al volo. Una volta che l'utente seleziona la scelta di una sezione aggiuntiva, basta impostare un flag per avere il metodo restituisce il numero di tabelle che si desidera. Poi si forza una ricarica del tavolo e si dovrebbe vedere la nuova sezione.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top