如何对字符串进行索引?- Swift 3

7 浏览
0 Comments

如何对字符串进行索引?- Swift 3

这个问题已经有答案了

在Swift中使用for-in创建字符串数组

在Swift中将Int转换为String

我是新手,需要您的帮助。请问如何更改此代码,以产生一个字符串数组而不是整数数组。感谢您的时间和任何帮助。

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var objTable: UITableView!
    var numberArray = NSMutableArray()
    var selectedArray=NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
        for index in 1...200 {
            numberArray.add(index)
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return numberArray.count;
    }
    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell
    {
        let contact = numberArray.object(at: indexPath.row)
        let cell:MyCustomClass = objTable.dequeueReusableCell(withIdentifier: "reuseCell") as! MyCustomClass
        cell.textLabel?.text = String("Number \(contact)")
        cell.tickButton.addTarget(self, action:#selector(ViewController.tickClicked(_:)), for: .touchUpInside)
        cell.tickButton.tag=indexPath.row
        if selectedArray .contains(numberArray.object(at: indexPath.row)) {
            cell.tickButton.setBackgroundImage(UIImage(named:"Select.png"), for: UIControlState())
        }
        else
        {
            cell.tickButton.setBackgroundImage(UIImage(named:"Diselect.png"), for: UIControlState())
        }
        return cell
    }
    func tickClicked(_ sender: UIButton!)
    {
        let value = sender.tag;
        if selectedArray.contains(numberArray.object(at: value))
        {
            selectedArray.remove(numberArray.object(at: value))
        }
        else
        {
            selectedArray.add(numberArray.object(at: value))
        }
        print("Selected Array \(selectedArray)")
    objTable.reloadData()
    }
    func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) ->CGFloat
    {
        return 80.0
    }
}

admin 更改状态以发布 2023年5月22日
0
0 Comments

如果您对高阶函数(如map函数)不熟悉,可以使用以下for循环...

var numberArray = [String]()
        for index in 1...200 {
            numberArray.append(String(index))
        }

这种方法的缺点是numberArray是可变的,因为它是一个var,大多数情况下应避免使用可变性。

这是正确的方法,您可以看到使用此方法numberArray是一个常量,我们不再改变其状态。

let numberArray = Array(1...200).map{String($0)}

0
0 Comments

试一下:

 let stringArray: [String] = (1...200).map {"Number " +  String(format: "%d", $0)}

然后删除这一行:

let contact = numberArray.object(at: indexPath.row)

并将这一行改变:

cell.textLabel?.text = String("Number \(contact)")

为:

cell.textLabel?.text = stringArray[indexPath.row]

更新:

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var objTable: UITableView!
//    var numberArray = NSMutableArray()
    let stringArray: [String] = (1...200).map {"Number " +  String(format: "%d", $0)}
    var selectedArray=NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
//        for index in 1...200 {
//            numberArray.add(index)
//        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
//        return numberArray.count;
        return stringArray.count;
    }
    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell
    {
//        let contact = numberArray.object(at: indexPath.row)
        let cell:MyCustomClass = objTable.dequeueReusableCell(withIdentifier: "reuseCell") as! MyCustomClass
//        cell.textLabel?.text = String("Number \(contact)")
        cell.textLabel?.text = stringArray[indexPath.row]
        cell.tickButton.addTarget(self, action:#selector(ViewController.tickClicked(_:)), for: .touchUpInside)
        cell.tickButton.tag = indexPath.row
        if selectedArray .contains(stringArray[indexPath.row]) {
            cell.tickButton.setBackgroundImage(UIImage(named:"Select.png"), for: UIControlState())
        }
        else
        {
            cell.tickButton.setBackgroundImage(UIImage(named:"Diselect.png"), for: UIControlState())
        }
        return cell
    }
    func tickClicked(_ sender: UIButton!)
    {
        let value = sender.tag;
        if selectedArray.contains(stringArray[value])
        {
            selectedArray.remove(stringArray[value])
        }
        else
        {
            selectedArray.add(stringArray[value])
        }
        print("Selected Array \(selectedArray)")
        objTable.reloadData()
    }
    func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) ->CGFloat
    {
        return 80.0
    }
}

0