调用self.tableView.reloadData需要10-15秒

时间:2015-01-19 22:02:01

标签: uitableview swift reloaddata eventkit ekeventkit

我正在尝试使用Swift将数据加载到表视图中。我的问题是显示数据需要10-15秒。数据最终会显示出来,但它只需要很长时间。我正在从iCal中检索事件,然后在表格视图中显示它们。代码有问题吗?

class EventsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var eventCounterLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!

    var eventStore = EKEventStore()
    var calendar: EKCalendar?
    var events = NSMutableArray() as NSMutableArray
    var times = NSMutableArray() as NSMutableArray

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        //Table View Data Source and Delegate
        self.tableView.delegate = self
        self.tableView.dataSource = self

        //Event Kit access
        eventStore.requestAccessToEntityType(EKEntityTypeEvent,
            completion: {(granted: Bool, error:NSError!) in
                if !granted {
                    println("Access to store not granted")
                }else{
                    self.getTheCalendar()
                }
        })
    }

    //Event Kit Methods
    func getTheCalendar(){
        //Set the start and finish dates for events
        let start = NSDate() as NSDate
        let finish = NSDate() as NSDate

        //Make the predicate
        let eventPredicate =     self.eventStore.predicateForEventsWithStartDate(start, endDate: finish, calendars: nil) as NSPredicate

       self.eventStore.enumerateEventsMatchingPredicate(eventPredicate, usingBlock: { (event: EKEvent!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
            if event != nil{
                self.events.addObject(event.title)
                self.times.addObject(event.startDate)
            }

            //Set the event counter label
            if self.events.count == 1{
                self.eventCounterLabel.text = "You have " + "\(self.events.count)" + " event today."
            }else{
                self.eventCounterLabel.text = "You have " + "\(self.events.count)" + " events today."
            }

            //Reload the table view
            self.tableView.reloadData()
        })
    }

    //Table View Methods
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.events.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath  indexPath: NSIndexPath) -> UITableViewCell {
        var cell:EventsTableViewCell =   self.tableView.dequeueReusableCellWithIdentifier("Cell") as   EventsTableViewCell

        //Set the event label
        cell.eventLabel.text = (self.events.objectAtIndex(indexPath.row) as String)

        cell.eventLabel.adjustsFontSizeToFitWidth = true

        //Set the event time label
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate:self.times.objectAtIndex(indexPath.row) as NSDate)

        let hour = components.hour as Int

        var theHourOfTheEvent = hour
        if theHourOfTheEvent < 12{

            if theHourOfTheEvent == 0{

                cell.eventTimeLabel.text = "All Day"

            }else{

                cell.eventTimeLabel.text = "\(theHourOfTheEvent)" + " am"

            }

        }else if theHourOfTheEvent > 12 && theHourOfTheEvent < 24{

            theHourOfTheEvent -= 12

            cell.eventTimeLabel.text = "\(theHourOfTheEvent)" + " pm"

        }else if theHourOfTheEvent == 24{

            theHourOfTheEvent -= 12

            cell.eventTimeLabel.text = "\(theHourOfTheEvent)" + " am"

        }else if theHourOfTheEvent > 24{

            theHourOfTheEvent -= 24

            cell.eventTimeLabel.text = "\(theHourOfTheEvent)" + " am"

        }else if theHourOfTheEvent == 12{

            cell.eventTimeLabel.text = "\(theHourOfTheEvent)" + " pm"

        }

        return cell
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 100.0
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    }
}

1 个答案:

答案 0 :(得分:2)

您正在重新加载EventKit枚举器内的表视图。

   self.eventStore.enumerateEventsMatchingPredicate(eventPredicate, usingBlock: { 
        ...
        self.tableView.reloadData()
    })

这意味着您为日历中的每个检索到的事件重新加载一次 - 这可能是很多不必要的重新加载,而且速度很慢。

在收集所有事件后,您应该只调用此函数一次。将它移到循环之外,你应该看到一个改进。

   self.eventStore.enumerateEventsMatchingPredicate(eventPredicate, usingBlock: { 
        ...
    })
    self.tableView.reloadData()

您可能还想在主线程上的调度块中调用它;更改UI的调用应始终在主线程上,并且从您的示例中不清楚执行代码的线程是什么。


或者,您可能希望在获得这些元素后立即将这些元素添加到表中 - 在这种情况下,请不要使用reloadTable,而是在获取新数据时使用insertRowsAtIndexPaths。这比每次调用reloadData要便宜,因为它不访问每个单元 - 实际上只会访问屏幕上的单元格,这很便宜,假设你在列表末尾添加新事件(而不是开始)。 / p>