作者kiii210 (Johnny.T)
看板MacDev
標題Re: [問題] UITableView動態增加資料
時間Thu May 23 00:59:56 2013
錯了,
insertRowsAtIndexPaths:withRowAnimation:
Inserts rows in the receiver at the locations identified by an array
of index paths, with an option to animate the insertion.
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation
Parameters
indexPaths
--> An array of NSIndexPath objects each representing a row
--> index and section index that together identify a row in the table view.
這個method是要讓你在指定的indexPath插入資料,
indexPaths這個陣列應該要是 NSIndexPath的陣列,
你的dataArray應該是tableView的dataSource...,
先在source插入資料沒錯,但是下一步要做的是把 NSIndexPath 用 NSArray 封裝起來
int count = [dataArray count];
NSIndexPath indexPath = [NSIndexPath indexPathForRow:count-2 section:0]
NSArray *array = [NSArray arrayWithObject:indexPath];
[tableView insertRowsAtIndexPaths:array withRowAnimation:blablabla];
這樣的code即是在倒數第二個row插入你要的資料~
以上是之前開發的經驗,有說錯還請板上各位糾正~~!
※ 引述《siriuschill (siriuschill)》之銘言:
: 我想自定一個Button
: 點擊下去後
: tableview會自動增加一行
: 上網找了好多資料
: 都是在navigation上點擊實現
: 有嘗試過寫到Button裡
: 還是不行
: [self.tableView reloadData]
: 還是不能刷新
: 請問各位大大
: 我是不是漏了什麼動作呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.248.95.215
→ Piceman:正解 05/23 01:02
→ kiii210:你還是把完整的code貼上來好了 05/23 11:56
→ yuanruo:看來我寫的你沒看到.. 05/23 12:51
→ siriuschill:yuanruo大大 我有把reloadData拿掉過 不過還是不行 05/23 15:59
→ yuanruo:我把你整段程式碼call過來 原因在你beginupdate寫錯地方 05/23 20:03
→ yuanruo:執行insertRowAtIndexPaths 你不加begin end 和reloadData 05/23 20:04
→ yuanruo:以你的例子 都去掉就ok了 05/23 20:04
→ yuanruo:你要寫beginUpdate 要寫在insert上面 完了後再endUpdate 05/23 20:05
推 siriuschill:非常感謝yuanruo大大,把那2行拿掉後就解決了(^o^) 05/23 22:15