2014-05-23 19 views
5

Jestem nowym programistą IOS i mam problem.Jak dynamicznie dodawać wiersze do określonej sekcji UITableView?

Mam UITableView z 2 sekcjami, jeden jest statyczny, a drugi dynamiczny.

W szczególnych działań muszę dodawać nowe wiersze do drugiego odcinka w czasie pracy ..

wiem, jak zarządzać UITableView, ale nie konkretny odcinek

może mi pomóc proszę?

Pozdrawiam wy wszyscy

+0

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

Odpowiedz

11

Można użyć insertRowsAtIndexPaths: metodę 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

to nie zadziałało, mogę dodać pusty wiersz do testu? lub moje źródło danych musi być gotowe do nowego wiersza? Mam dwie sekcje, pierwsza to 0 lub 1? – vmfesta

+0

Musisz ustawić źródło danych przed wstawieniem nowego wiersza (do testowania możesz dodać obojętny obiekt w określonym indeksie i sprawdzić). Jeśli masz dwie sekcje, pierwsza będzie wynosić 0, a druga będzie 1. – Akhilrajtr

+0

Nie używam źródła danych dla moich sekcji, jak powiedziałem, pomysł na pierwszy jest statyczny, a na drugi dynamiczny, próbowałem zaimplementować źródło danych dla dynamicznego, ale kiedy go przetestuję, obie sekcje są wypełniane źródłem danych ... jak mogę zachować dane w pierwszym? – vmfesta

1

Możesz używać tej samej metody insertRowAtIndexPath jak

// 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]; 

to będzie wstawić dwa rzędy w ust.1. Pamiętaj, zanim to zrobisz, zarządzaj swoim obiektem źródła danych.

0

Można to zrobić za pomocą metody scrolltoinfinite ale przed tym u trzeba importować 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]; 
} 

tutaj indexpaths1 jest tablicą indexpaths kolejnych komórek, które chcesz wstawić do tabeli. spróbuj użyć pętli, aby uzyskać następny zestaw tablic i zapisać je w indeksach1.

+0

Wartość ** CGFloat heigh ** = self.tableViewMixedFeed.frame.size.wysokość nie jest używana – Malder

-1

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

użyć tej metody ..

+1

Cześć asdafd, to wygląda na potwierdzenie Anil Varghese odpowiedź, proszę nie dodawaj tego jako odpowiedź. – bummi

0

Swift 3 wersja

// 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() 
Powiązane problemy