2014-05-23 10 views
5

Sono un nuovo programmatore IOS e sto riscontrando un problema.Come aggiungere dinamicamente le righe a una sezione specifica di UITableView?

Ho un UITableView con 2 sezioni, uno è statico e un altro è dinamico.

In azioni specifiche ho bisogno di aggiungere nuove righe per la seconda sezione in fase di esecuzione ..

so come gestire un UITableView, ma non una sezione specifica

mi può aiutare per favore?

migliore che saluti tutti

+0

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageInsertDeleteRow/ManageInsertDeleteRow. html – iPatel

risposta

11

È possibile utilizzare insertRowsAtIndexPaths: metodo UITableView

//Update data source with the object that you need to add 
[tableDataSource addObject:newObject]; 

NSInteger row = //specify a row where you need to add new row 
NSInteger section = //specify the section where the new row to be added, 
//section = 1 here since you need to add row at second section 

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
[self.tableView endUpdates]; 
+0

non ha funzionato, posso aggiungere una riga vuota per il test? o la mia origine dati deve essere pronta per la nuova riga? Ho due sezioni, la prima è 0 o 1? – vmfesta

+0

È necessario impostare l'origine dati prima di inserire una nuova riga (per i test è possibile aggiungere un oggetto fittizio all'indice specifico e verificare). Se hai due sezioni, la prima sarà 0 e la seconda sarà 1. – Akhilrajtr

+0

Non sto utilizzando un'origine dati per le mie sezioni, come ho detto, l'idea per la prima è di essere statica, e per la seconda una dinamico, ho provato a implementare un'origine dati per quella dinamica, ma quando lo collaudo, entrambe le sezioni si riempiono di dati sorgente ... come posso mantenere i dati nel primo? – vmfesta

1

È possibile utilizzare lo stesso metodo insertRowAtIndexPath come

// Add the items into your datasource object first. Other wise you will end up with error 
// Manage the number of items in section. Then do 

NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:1]; 
NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:1 inSection:1]; 
[self.tableView insertRowsAtIndexPaths:@[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop]; 

Questo sarà inserire due righe alla sezione 1. Ricorda prima di fare ciò che hai gestito il tuo oggetto datasource.

0

è possibile farlo utilizzando il metodo scrolltoinfinite ma prima che u avere importare 3rd party svpulltorefresh

[self.tableViewMixedFeed addInfiniteScrollingWithActionHandler:^{ 

     CGFloat height = self.tableViewMixedFeed.frame.size.height; 

     CGFloat contentYoffset = self.tableViewMixedFeed.contentOffset.y; 

     CGFloat distanceFromBottom = self.tableViewMixedFeed.contentSize.height - contentYoffset; 
if(distanceFromBottom<contentYoffSet) 
{ 
[weak insertRowAtBottom]; 
} 
}]; 

-(void)insertRowAtBottom 
{ 
[self.tableViewMixedFeed beginUpdates]; 
    [self.tableViewMixedFeed insertRowsAtIndexPaths:indexPaths1 withRowAnimation:UITableViewRowAnimationTop]; 

    [self.tableViewMixedFeed endUpdates]; 
    [self.tableViewMixedFeed.infiniteScrollingView stopAnimating]; 
} 

qui indexpaths1 è la matrice di indexpaths delle celle successive che vuoi inserire in una tabella. prova ad usare il loop per ottenere il prossimo set di array e memorizzarli in indexpaths1.

+0

Valore ** Altezza CGFloat ** = self.tableViewMixedFeed.frame.size.altezza non utilizzata – Malder

-1

[self.tableView insertRowsAtIndexPaths: @ [indexPath1, indexPath2] withRowAnimation: UITableViewRowAnimationTop];

utilizzare questo metodo ..

+1

Ciao asdafd, questa sembra una conferma della risposta di Anil Varghese, per favore non aggiungerla come risposta. – bummi

0

Swift 3 versione

// Adding new item to your data source 
dataSource.append(item) 
// Appending new item to table view 
yourTableView.beginUpdates() 
// Creating indexpath for the new item 
let indexPath = IndexPath(row: dataSource.count - 1, section: yourSection) 
// Inserting new row, automatic will let iOS to use appropriate animation 
yourTableView.insertRows(at: [indexPath], with: .automatic) 
yourTableView.endUpdates() 
Problemi correlati