gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:18:10 26 4
gpt4 key购买 nike

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

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) {

}
}

最佳答案

您正在 EventKit 枚举器中重新加载 TableView 。

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

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

在收集完所有事件后,您应该只调用此函数一次。将其移出循环,您应该会看到改进。

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

您可能还想在主线程的调度 block 中调用它;更改 UI 的调用应始终在主线程上进行,从您的示例中不清楚哪个线程正在执行代码。


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

关于uitableview - 调用 self.tableView.reloadData 需要 10-15 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28034252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com