Database Reference
In-Depth Information
//MARK - Helper methods
func
fetchAndReload(){
var
error:
NSError
?
let
results =
coreDataStack
.
context
.
executeFetchRequest
(
fetchRequest
,
error: &error)
as
[
Venue
]?
if
let
fetchedResults = results {
venues
= fetchedResults
}
else
{
println
(
"Could not fetch
\(error)
,
\(error!.
userInfo
)
"
)
}
tableView
.
reloadData
()
}
As its name suggests,
fetchAndReload()
executes the fetch request and reloads the
table view. Other methods in this class will need to see the fetched objects, so you
store the fetched results in the
venues
property you defined earlier.
There's one more thing you have to do before you can run the sample project for
the first time: You have to hook up the table view's data source with the fetched
Venue
objects.
Replace the placeholder implementations of
numberOfRowsInSection
and
cellForRowAtIndexPath
with the following:
func
tableView(tableView:
UITableView
?,
numberOfRowsInSection section:
Int
) ->
Int
{
return
venues
.
count
}
func
tableView(tableView:
UITableView
!,
cellForRowAtIndexPath
indexPath:
NSIndexPath
!) ->
UITableViewCell
! {
var
cell =
tableView.
dequeueReusableCellWithIdentifier
(
"VenueCell"
)
as
UITableViewCell
let
venue =
venues
[indexPath.
row
]
cell.
textLabel
!.
text
= venue.
name
cell.
detailTextLabel
!.
text
= venue.
priceInfo
.
priceCategory
return
cell