小编J.V*_*ehr的帖子

致命错误:索引超出范围

我想在我的图书馆中播放25首歌曲.这是我的代码:

var allSongsArray: [MPMediaItem] = []
let songsQuery = MPMediaQuery.songsQuery()

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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell")
    let items = allSongsArray[indexPath.row]
    cell?.textLabel?.text = items.title
    cell?.detailTextLabel?.text = items.artist
    cell?.imageView?.image = items.artwork?.imageWithSize(imageSize)

    return cell!
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它会崩溃,因为:

致命错误:索引超出范围

我试图更改numberOfRowsInSectionallSongsArray.count,但最终会出现相同的错误.

uitableview ios swift

14
推荐指数
1
解决办法
5万
查看次数

如何隐藏用于特定文本字段输入的标签?

我创建了一个只有一个按钮,一个文本字段和一个标签的Xcode项目。当文本字段输入为“ test123”或按钮被按下时,我只想使标签可见。带按钮的部分效果很好,但是无论我在文本字段中键入什么内容,标签都保持隐藏状态。有人帮忙吗?这是我当前的代码:

    import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var Label: UILabel!
    @IBOutlet weak var Examplefield: UITextField!


    override func viewDidLoad() {

               super.viewDidLoad()
               // Do any additional setup after loading the view, typically from a nib.

               Label.hidden = true

        Examplefield.resignFirstResponder()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func ButtonPressed(sender: UIButton) {

        Label.hidden = false
    }

    func textFieldDidEndEditing(textField: UITextField) {
        if Examplefield.text == "test123" {
            Label.hidden = false

    }
        else …
Run Code Online (Sandbox Code Playgroud)

xcode ios swift

1
推荐指数
1
解决办法
5009
查看次数

标签 统计

ios ×2

swift ×2

uitableview ×1

xcode ×1