我有一个数组var previousSongs = [PreviousSong]()
。
此数组中填充了名为getSongs()
的函数中的信息。我知道信息正确地放入previousSongs
数组
我想在表格中使用此数组中的信息。
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.contentInset = UIEdgeInsetsMake(4,0,0,0);
self.tableView.dataSource = self
previousSongs = [PreviousSong(song: "NAME1", artist: "ARTIST1", time: "TIME1"), PreviousSong(song: "NAME2", artist: "ARTIST2", time: "TIME2"), PreviousSong(song: "NAME3", artist: "ARTIST3", time: "TIME3"), PreviousSong(song: "NAME4", artist: "ARTIST4", time: "TIME4"), PreviousSong(song: "NAME5", artist: "ARTIST5", time: "TIME5"), PreviousSong(song: "NAME6", artist: "ARTIST6", time: "TIME6"), PreviousSong(song: "NAME7", artist: "ARTIST7", time: "TIME7"), PreviousSong(song: "NAME8", artist: "ARTIST8", time: "TIME8"), PreviousSong(song: "NAME9", artist: "ARTIST9", time: "TIME9"), PreviousSong(song: "NAME10", artist: "ARTIST10", time: "TIME10"), ]
getSongs()
self.tableView.reloadData()
}
func getSongs() {
let url = NSURL(string: "http://api.vicradio.org/songs/history?amount=10")!
let request = NSMutableURLRequest(URL: url)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { (data: NSData?, response: NSURLResponse?, error: NSError?) in
if error != nil {
return
}
let name = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
var songName = ""
var artistName = ""
var quoteNumber = 0
var braceNumber = 0
for character in name.characters {
if braceNumber % 2 == 0 && braceNumber != 0 {
if songName != "" {
quoteNumber = 0
self.previousSongs.append(PreviousSong(song: songName, artist: artistName, time: "TIME"))
songName = ""
artistName = ""
}
}
if character == "{" || character == "}" {
braceNumber++
}
if character == "\"" {
quoteNumber++
}
if quoteNumber == 3 && character != "\"" {
artistName += String(character)
} else if quoteNumber == 7 && character != "\"" {
songName += String(character)
}
}
dispatch_async(dispatch_get_main_queue()) { self.getSongs() }
}
if self.previousSongs.count == 10 {
task!.cancel()
} else {
task!.resume()
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
var song : PreviousSong
song = previousSongs[indexPath.row]
cell.textLabel?.text = song.song + " by " + song.artist
cell.detailTextLabel?.text = "Played at " + song.time
return cell
}
该表将显示硬编码数组([PreviousSong(song: "NAME1", artist: "ARTIST1",
...),但在getSongs()
函数中更新后,它不会加载数组中的信息。
如何解决这个问题,以便当我使用getSongs()
函数中的实际信息更新数组时,单元格将使用正确的信息重新加载? (我已经尝试过使用reloadData()方法。)
答案 0 :(得分:0)
肯定听起来你的问题出在getSongs()
函数中。你介意提供你的实施吗?
在我的脑海中,可能至少有一个正在发生:previousSongs
在getSongs()
中被设置为nil或为空,你的行数数据源方法没有返回正确的号码(previousSongs.count
)。
编辑:查看代码后,为什么不让getSongs()
函数返回数组,然后在数组中使用lazy
关键字 - 然后调用getSongs()
函数previousSongs
数组的实例化?这样,就无需在viewDidLoad()
上重新加载表格。很确定整件事情会好得多。
答案 1 :(得分:0)
替换:
dispatch_async(dispatch_get_main_queue()) {
self.getSongs()
}
使用:
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
您没有理由在getSongs()
内拨打getSongs()
。您希望在处理完数据后使用reloadData()
重新加载表。 <{1}}中的reloadData()
在数据完全处理后不会执行。
答案 2 :(得分:0)
这是我找到的一个解决方案!
不使用getSongs()
附加到数组,而是使用.insert
方法。我认为追加是将我想要的信息添加到数组的末尾。数据正确加载,但它不是我想要的数据。