作者strife00 (strifecloud)
看板MacDev
標題Re: [問題] 想問有關UIRefreshControl
時間Thu Aug 6 20:55:53 2015
不好意思,經版友提醒,貼上我的loadData function如下:
@IBAction func loadData(){
timelineData.removeAll(keepCapacity: false);
var findTimelineData: PFQuery = PFQuery(className: "Sweets");
findTimelineData.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]!, error: NSError!) -> Void in
if error == nil{
self.timelineData = objects.reverse() as! [PFObject]
self.tableView.reloadData();
}
}
}
並補充Xcode指出錯誤的行數如下:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell: SweetTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SweetTableViewCell;
Xcode說這行錯了-> let sweet: PFObject = self.timelineData[indexPath.row] as PFObject;
cell.sweetTextView.text = sweet.objectForKey("content") as! String;
cell.titleTextView.text = sweet.objectForKey("title") as! String;
cell.location.setTitle((sweet.objectForKey("location")) as? String, forState: UIControlState.Normal);
可能是這邊有問題,但我不太知道是哪邊有問題,希望得到解決,感謝!!
※ 引述《strife00 (strifecloud)》之銘言:
: 大家好
: 小弟做了一個UITableViewController,裡面UITableViewCell資料是連接Parse取得
: 想用UIRefreshController來做往下拉的更新
: 加入UIRefreshControl後第一次往下拉更新有成功
: 但是第二次再往下拉的話,就會出現“fatal error: Array index out of range”
: 每次我加入新資料後,第一次往下拉更新都成功,但是只要再加入新資料,
: 第二次更新就一定會出現上面的問題,想請問各位大大要如何解決呢?
: (之前還沒加入UIRefreshControl時,運作都很正常,
: 我寫了一個methods作為更新的按鈕之用,但因為這次想用下拉式更新)
: 希望能解決這個問題,感謝
: 我加入UIRefreshControll的程式碼如下:
: viewDidLoad() {
: super.viewDidLoad()
: self.refreshControl = UIRefreshControl();
: self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull to refresh");
: self.refreshControl?.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
: }
: func refresh(sender:AnyObject){
: println("refresh")
: self.loadData();
: self.refreshControl?.endRefreshing();
: }
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.38.214
※ 文章網址: https://www.ptt.cc/bbs/MacDev/M.1438865756.A.9F4.html
※ 編輯: strife00 (123.193.38.214), 08/06/2015 21:23:26
→ darktt: 請提供XCode報錯的訊息內容, 08/06 21:55
→ darktt: 另外PFObject有符合KVC架構 所以可以直接使用sweet["Key"] 08/06 21:56
→ darktt: 不過還是建議將一個DB的Table建立一個Class會比較好管理 08/06 21:57
→ darktt: 當你有數個Table與數十個Column的時候,你會感謝你之前的 08/06 21:58
→ darktt: 努力的 08/06 21:58
→ howdiun: 可能是indexPath.row的數值超過array的大小 08/07 09:21
→ gradyzhuo: 會發生這種情況 通常是你存data的Array太早清空了 UICo 08/08 14:14
→ gradyzhuo: ntrol的觸發有一個奇特的狀況 他會假定你的資料存在的 08/08 14:14
→ gradyzhuo: 狀況下 重跑一次tableView:cellForIndexPath: 而且是 08/08 14:14
→ gradyzhuo: 跳過numberOfRowInSection: 所以回到你的狀況 你的time 08/08 14:14
→ gradyzhuo: lineData.removeAll 太早做了 你的狀況可以試試看把那 08/08 14:14
→ gradyzhuo: 行註解掉 再執行看看 08/08 14:14
→ gradyzhuo: 更正: UIControl->UIRefreshControl 08/08 14:15
推 gradyzhuo: 補充一下 主詞沒說清楚 「他會假定你的資料存在的...」 08/08 14:19
→ gradyzhuo: 這裡的他是TableView 08/08 14:19
→ strife00: 解決問題了!感覺大大們的建議! 08/12 15:19
→ strife00: 把.removeAll那行移到下面的if裡就可以正常運作了 08/12 15:20