2015-02-10 27 views
0

即时编写应用程序,但无法弄清楚如何将此特定的Objective-C行转换为swift。任何帮助都感激不尽。我已经能够翻译其他行很好,但这一个抛出一些异常错误。这是原始的Objective-C行:应用程序只显示一个单元格(一首歌曲)而不是所有的单元格

MPMediaItem * rowItem = [songs objectAtIndex:indexPath.row];

我一个小白和已经尝试了许多变化,包括:

变种rowItem = songs.objectAtIndex(indexPath.row)作为MPMediaItem

var rowItem:MPMediaItem = songs.objectAtIndex(indexPath.row)as MPMediaItem

但我一直在这么长时间,我看不到它应该是什么。非常感谢!

编辑:

歌曲被定义为:

让songsQuery = MPMediaQuery.songsQuery()
让歌曲= [songsQuery.items]如NSArray的

我刚接收一个exe_breakpoint错误。如果我使用...作为[MPMediaItem]将MPMediaItem封装在数组中,但我无法获得属性值,例如MPMediaItemPropertyTitle和MPMediaItemPropertyArtist。

更新: 通过使用for循环获取MPMediaItemPropertyTitle和MPMediaPropertyArtist。 “歌曲”变量的定义如上所述。 @Antonio,删除括号不起作用。这是我当前的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 

    // Configure the cell... 

    let songsQuery = MPMediaQuery.songsQuery() 
    let allSongsArray = [songsQuery.items] as NSArray 

    var singleItem: AnyObject = allSongsArray[indexPath.row] 

    var rowItem = singleItem as [MPMediaItem] 

    if rowItem.count > 0 { 

     cell.textLabel?.text = rowItem[0].valueForProperty(MPMediaItemPropertyTitle) as NSString 
     cell.detailTextLabel?.text = rowItem[0].valueForProperty(MPMediaItemPropertyArtist) as NSString 
    } 

    return cell 
} 

的问题,我现在是当我测试的应用程序,它仅显示非常的第一首歌曲在我的图书馆,并像它应该不会列出每首歌曲。感谢您提供任何帮助!

更新2:

得到它用下面的代码工作:

class SongsViewController: UITableViewController { 

var allSongs : [MPMediaItem] = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    allSongs += MPMediaQuery.songsQuery().items as [MPMediaItem] 
    println(allSongs.count) 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 

    return allSongs.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

    // Configure the cell... 

    var rowItem = allSongs[indexPath.row] 

    cell.textLabel?.text = rowItem.valueForProperty(MPMediaItemPropertyTitle) as NSString 
    cell.detailTextLabel?.text = rowItem.valueForProperty(MPMediaItemPropertyArtist) as NSString 

    return cell 
} 
+0

你应该提供更多的细节 - 如何定义'songs'?你有什么样的错误?编译或运行时?什么是错误信息? – Antonio 2015-02-10 17:41:14

+0

@Antonio感谢您的回复,我编辑了原帖。 – air6199 2015-02-10 17:46:59

+0

为什么那些方括号'[songsQuery.items]'? items属性的数据类型是什么? – Shoaib 2015-02-10 17:53:12

回答

0
var allSongs : [MPMediaItem] = [] 

override func viewDidLoad() { 
super.viewDidLoad() 

// Uncomment the following line to preserve selection between presentations 
// self.clearsSelectionOnViewWillAppear = false 

allSongs += MPMediaQuery.songsQuery().items as [MPMediaItem] 
println(allSongs.count) 

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
// self.navigationItem.rightBarButtonItem = self.editButtonItem() 
} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 

    return allSongs.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

    // Configure the cell... 

var rowItem = allSongs[indexPath.row] 

cell.textLabel?.text = rowItem.valueForProperty(MPMediaItemPropertyTitle) as NSString 
cell.detailTextLabel?.text = rowItem.valueForProperty(MPMediaItemPropertyArtist) as NSString 

return cell 

}

0
let allSongsArray = [songsQuery.items] as NSArray 

在这里你采取Array<AnyObject>!而在另一个Array然后您可以转换为一个NSArray它包...相当混乱。

试试这个:

let songsQuery = MPMediaQuery.songsQuery() 
let allSongs = songsQuery.items as [MPMediaItem] 
let song = allSongs[indexPath.row] 

cell.textLabel?.text = song.valueForProperty(MPMediaItemPropertyTitle) as String 
cell.detailTextLabel?.text = song.valueForProperty(MPMediaItemPropertyArtist) as String 

而且,你真的应该缓存allSongs不是每则需要填充细胞时查询它的。为此,只需创建一个var allSongs: [MPMediaItem] = []到您的控制器。然后在例如viewDidLoad中使用MPMediaQuery来填充该数组。然后,您可以在任何表视图委托和数据源方法中使用该数组。

+0

不,不起作用。 “数组索引超出范围”的线让歌曲:AnyObject = ...(我也假定allSongsArray应该是allSongs)我将如何缓存歌曲列表? – air6199 2015-02-10 18:41:34

+0

好吧,所以表中的行数可能与'allSongs'中的项数不同。你的numberOfRowsInSection ...方法做什么? (你对'allSongsArray'是正确的,我修正了它) – Rengers 2015-02-10 18:43:13

+0

它执行一个songsQuery并将它们放入一个数组(与前两行代码发布相同的方法),然后它返回allSongsArray.count – air6199 2015-02-10 18:47:06

0

我认为你所做的错误是加载整个数据源cellForRowAtIndexPath,而这应该在另一个地方完成(viewDidLoad是一个很好的候选人),最重要的一次,而不是每一行。

我返工你的代码,我想出了这个代码:

class TestViewController : UITableViewController { 
    var allSongsArray: [MPMediaItem]! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let songsQuery = MPMediaQuery.songsQuery() 
     self.allSongsArray = songsQuery.items as [MPMediaItem] 

     // Other initializations 
     ... 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.allSongsArray.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 

     var rowItem = self.allSongsArray[indexPath.row] 

     cell.textLabel?.text = rowItem[0].valueForProperty(MPMediaItemPropertyTitle) as NSString 
     cell.detailTextLabel?.text = rowItem[0].valueForProperty(MPMediaItemPropertyArtist) as NSString 

     return cell 
    } 
} 
+0

设法让我的工作大约10秒钟,然后再发布!我会在主帖中发布相关代码,请你看看我是否已经在正确的地方存储/加载了数据? – air6199 2015-02-10 19:30:38

+0

@ air6199你的代码看起来不错,实际上我的看起来像你的复印件;-) – Antonio 2015-02-10 19:44:16

相关问题