我是一個新的IOS程序員,我有一個問題。如何將行動態添加到特定的UITableView部分?
我有一個UITableView有2個部分,一個是靜態的,另一個是動態的。
在我需要在運行時的第二部分中添加新行的具體行動..
我知道如何管理一個UITableView,但不是一個特定部分
你能幫助我嗎?
問候大家
我是一個新的IOS程序員,我有一個問題。如何將行動態添加到特定的UITableView部分?
我有一個UITableView有2個部分,一個是靜態的,另一個是動態的。
在我需要在運行時的第二部分中添加新行的具體行動..
我知道如何管理一個UITableView,但不是一個特定部分
你能幫助我嗎?
問候大家
您可以使用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或1? – vmfesta
您需要在插入新行之前設置數據源(爲了測試,您可以在特定索引處添加一個虛擬對象並進行檢查)。如果你有兩部分,第一部分將是0,第二部分將是1. – Akhilrajtr
我沒有爲我的部分使用數據源,正如我所說的,第一部分的想法是靜態的,第二部分動態的,我試圖爲動態的實現數據源,但是當我測試它時,兩個部分都充滿了數據源......我如何將數據保存在第一個數據中? – vmfesta
您可以用同樣的方法insertRowAtIndexPath
像
// 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];
這將插入兩行到SECTION1。記住在做這個之前你已經管理了你的數據源對象。
insertRowsAtIndexPaths:
方法,你可以通過使用scrolltoinfinite方法做到這一點,但在此之前,u必須導入第三方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];
}
這裏indexpaths1是indexpaths的陣列您想要插入表中的下一個單元格。 嘗試使用循環獲取下一組數組並將它們存儲到indexpaths1中。
值** CGFloat高度** = self.tableViewMixedFeed.frame.size。高度不使用 – Malder
[self.tableView insertRowsAtIndexPaths:@ [indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];
使用此方法..
嗨asdafd,這看起來像Anil Varghese答案的確認,請不要將此添加爲答案。 – bummi
斯威夫特3版本
// 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()
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageInsertDeleteRow/ManageInsertDeleteRow。 html – iPatel