Swift를 사용하여 테이블 뷰에 데이터를로드하려고합니다. 내 문제는 데이터를 표시하는 데 10-15 초가 걸린다는 점입니다. 데이터가 표시되는 것을 끝내지 만 오랜 시간이 걸립니다. iCal에서 이벤트를 가져 와서 테이블보기로 표시하고 있습니다. 코드에 문제가 있습니까?self.tableView.reloadData 호출시 10-15 초 소요
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) {
}
}
메인 스레드의 디스패치 블록에서 호출하는 것이 효과적이었습니다! – PoKoBros