我對創建應用程序非常陌生,我從Swift開始iOS開發。我真的希望有人會花一點時間來幫助我,因爲我現在很困難。所以我正在創建一個帶有主細節tableview的應用程序。我將使用Alamofire從外部獲取數據,並且返回的數據將是JSON,我可以使用SwiftyJSON進行分析。UITableView切片
我能夠顯示數據就好,這對我來說工作得很好。但是我想實現的目標以及我無法理解的東西是如何用titleForHeaderInSection
按字母順序排列我的項目,並且能夠使用sectionIndexTitlesForTableView
導航。
從我的API接收的數據看起來是這樣的(格式可改變,因爲我控制API):
{
"A": [
{
"id": "1",
"char": "A",
"title": "ABC",
"body": "Denne sang hedder ABC"
},
{
"id": "2",
"char": "A",
"title": "Abel spandabel",
"body": "Denne sang hedder Abel spandabel"
},
{
"id": "3",
"char": "A",
"title": "Aha aha aha",
"body": "Denne sang hedder Aha aha aha"
}
],
"B": [
{
"id": "4",
"char": "B",
"title": "Bussemand",
"body": "Denne sang hedder Bussemand"
},
{
"id": "5",
"char": "B",
"title": "Balademager",
"body": "Denne sang hedder Balademager"
}
],
"D": [
{
"id": "6",
"char": "D",
"title": "Dukke mand",
"body": "Denne sang hedder Dukke mand"
},
{
"id": "7",
"char": "D",
"title": "Dansevisen",
"body": "Denne sang hedder Dansevisen"
}
]
}
然後,我有一個Song.swift
文件定義了一些屬性,一個結構我個別項目。我看起來是這樣的:
struct Song {
var title : String
var body : String
var char : String
}
最後,我有一個實現以繪製的數據的tableview必要的邏輯視圖控制器返回
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var songsTableView: UITableView!
var songs = [Song]()
func addSong(song: Song) {
self.songs.append(song)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return songs.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
var currentSong = songs[indexPath.row]
cell.textLabel?.text = currentSong.title
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var nextScreen = segue.destinationViewController as! SingleViewController
if let indexPath = self.songsTableView.indexPathForSelectedRow() {
let selectedSong = songs[indexPath.row]
nextScreen.currentSong = selectedSong
}
}
func getSongs() {
Alamofire.request(.GET, "http://MYAPI.COM")
.responseJSON { (_, _, data, _) in
let json = JSON(data!)
// TODO: Come up with some better names for the "subJson"
for (key: String, subJson: JSON) in json {
// TODO: Come up with some better names for the "subSubJson"
for (key: String, subSubJson: JSON) in subJson {
var title: String = subSubJson["title"].string!
var body: String = subSubJson["body"].string!
var char: String = subSubJson["char"].string!
var song = Song(title: title, body: body, char: char)
self.addSong(song)
}
}
self.songs.sort({$0.title < $1.title})
self.songsTableView!.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.getSongs()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我知道這也許應該是教科書東西,但我現在已經呆了三天了,我開始有點失去主意了。所以我真的希望有人會花時間幫助我,並解決一些我可能做錯的事
在此先感謝!
爲什麼不使用'NSJSONSerialization'的JSON解析?自iOS 5起可用。 –