2014-05-23 12 views
5

Tôi là một Lập trình viên IOS mới và tôi đang gặp sự cố.Cách tự động thêm hàng vào một phần UITableView cụ thể?

Tôi có UITableView với 2 phần, một phần là tĩnh và một phần khác là động.

Trong hành động cụ thể tôi cần phải thêm hàng mới cho phần thứ hai trong thời gian chạy ..

tôi biết làm thế nào để quản lý một UITableView, nhưng không phải là một phần cụ thể

Ông có thể giúp tôi xin vui lòng?

Trân trọng tất cả các bạn

+0

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

Trả lời

11

Bạn có thể sử dụng insertRowsAtIndexPaths: phương pháp 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

nó không hoạt động, tôi có thể thêm một hàng trống để kiểm tra? hoặc nguồn dữ liệu của tôi cần phải sẵn sàng cho hàng mới? Tôi có hai phần, phần đầu tiên là 0 hoặc 1? – vmfesta

+0

Bạn cần đặt nguồn dữ liệu sẵn sàng trước khi chèn hàng mới (để kiểm tra bạn có thể thêm đối tượng giả vào chỉ mục và kiểm tra cụ thể). Nếu bạn có hai phần, phần đầu tiên sẽ là 0 và phần thứ hai sẽ là 0 và số thứ hai sẽ là 1. – Akhilrajtr

+0

tôi không sử dụng nguồn dữ liệu cho các phần của mình, như tôi đã nói, ý tưởng cho phần đầu tiên là tĩnh, và thứ hai là năng động, tôi đã cố gắng để thực hiện một nguồn dữ liệu cho một động, nhưng khi tôi kiểm tra nó, cả hai phần đang được làm đầy với nguồn dữ liệu ... làm thế nào tôi có thể giữ dữ liệu trong một đầu tiên? – vmfesta

1

Bạn có thể sử dụng cùng một phương pháp insertRowAtIndexPath như

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

này sẽ chèn hai hàng đến phần1. Hãy nhớ trước khi thực hiện việc này, bạn đã quản lý đối tượng nguồn dữ liệu của mình.

0

bạn có thể làm điều này bằng cách sử dụng phương pháp scrolltoinfinite nhưng trước đó u phải nhập khẩu bên thứ 3 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]; 
} 

đây indexpaths1 là mảng indexpaths các ô tiếp theo mà bạn muốn chèn vào bảng. thử sử dụng vòng lặp để lấy tập hợp các mảng tiếp theo và lưu chúng vào trong đường dẫn chỉ mục1.

+0

Giá trị ** CGFloat heigh ** = self.tableViewMixedFeed.frame.size.chiều cao không được sử dụng – Malder

-1

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

SỬ DỤNG PHƯƠNG PHÁP NÀY ..

+1

Hi asdafd, điều này giống như một xác nhận của câu trả lời Anil Varghese, xin vui lòng không thêm này như là câu trả lời. – bummi

0

Swift 3 phiên bản

// 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() 
Các vấn đề liên quan