簡體   English   中英

從未在UITableViewController中調用cellForRowAtIndexPath

[英]cellForRowAtIndexPath never called in UITableViewController

永遠不會使用UITableViewController以下列方式調用cellForRowAtIndexPath(numberOfRowsInSection):

import UIKit
import EventKit

class EventThisWeekController: UITableViewController {

    var eventStore: EKEventStore = EKEventStore()
    var eventsThisWeek: [EKEvent] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        self.eventStore.requestAccessToEntityType(.Event) {
            (granted, error) -> Void in

            guard granted else { fatalError("Permission denied") }

            let endDate = NSDate(timeIntervalSinceNow: 604800)
            let predicate = self.eventStore.predicateForEventsWithStartDate(NSDate(),
                                                                            endDate: endDate,
                                                                            calendars: nil)
            self.eventsThisWeek = self.eventStore.eventsMatchingPredicate(predicate)
            print(self.eventsThisWeek.count)
        }

    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        print("cellForRowAtIndexPath")
        let cell = UITableViewCell(style: .Default, reuseIdentifier: nil)

        cell.textLabel?.text = self.eventsThisWeek[indexPath.row].title

        return cell
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("numberOfRowsInSection")
        return eventsThisWeek.count
    }
}

數據源和委托分配給此Controller,EvenThisWeekController是主故事板中設計的類。 結果,在我的app表中顯示沒有任何結果。 當然,eventThisWeek數組長度不等於0.任何想法我怎么能解決它?

用this替換viewDidLoad中的代碼。

self.eventStore.requestAccessToEntityType(.Event) {
    (granted, error) -> Void in

    guard granted else { fatalError("Permission denied") }

    let endDate = NSDate(timeIntervalSinceNow: 604800)
    let predicate = self.eventStore.predicateForEventsWithStartDate(NSDate(),
                                                                    endDate: endDate,
                                                                    calendars: nil)
    self.eventsThisWeek = self.eventStore.eventsMatchingPredicate(predicate)
    dispatch_async(dispatch_get_main_queue(),{

        self.tableView.reloadData()

    })
    print(self.eventsThisWeek.count)
 }

你的cellForRowAtIndexPath沒有調用,因為當你的視圖加載時,數組是空的,當self.eventStore.requestAccessToEntityType完全執行並加載具有新值的數組時,你沒有通知你的tableView重新加載數據數組中的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM