我如何能够创建一个 tableViewCell 以按字母顺序显示按文本字段添加的文本?
How am I able to create a tableViewCell that shows added texts by textfield alphabetically?
我使用文本字段将文本添加到 tableViewCell 中,但在 tableView 顶部的单元格中输入的单词顺序不是字母顺序。它是如何按字母顺序排列的?是否有任何内置方法可以执行此操作,或者我是否编写了一个方法?谢谢!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var myTextField: UITextField!
var stringArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stringArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = stringArray[indexPath.row]
return cell
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
return true
}
@IBAction func AddButton(sender: UIButton) {
stringArray.append(myTextField.text)
myTextField.text = nil
myTextField.resignFirstResponder()
tableView.reloadData()
}
}
UITableView
只是向您显示数组中的相同字符串。您正在使用 append
方法,该方法总是将项目添加到数组的末尾。相反,您应该对数组进行排序,UITableView
中显示的数据将被排序。
数组有 sort
个,您为其提供了比较闭包。您可以制作一个排序方法并将其作为 isOrderedBefore
参数传递给 sort
,或者您可以这样做。
stringArray.sort(){ [=10=] < }
其中 [=17=]
和
是将要比较的第一个和第二个参数。
对于类似排序顺序的 Finder,您可以使用 localizedStandardCompare。
stringArray.sortInPlace { (x, y) -> Bool in
return x.localizedStandardCompare(y) == NSComparisonResult.OrderedAscending
}
还要添加到@hannad 的答案中,您应该使用sortInPlace 来确保对stringArray 进行排序。否则使用类似
sortedArray = stringArray.sort(){ [=11=] < }
我使用文本字段将文本添加到 tableViewCell 中,但在 tableView 顶部的单元格中输入的单词顺序不是字母顺序。它是如何按字母顺序排列的?是否有任何内置方法可以执行此操作,或者我是否编写了一个方法?谢谢!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var myTextField: UITextField!
var stringArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stringArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = stringArray[indexPath.row]
return cell
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
return true
}
@IBAction func AddButton(sender: UIButton) {
stringArray.append(myTextField.text)
myTextField.text = nil
myTextField.resignFirstResponder()
tableView.reloadData()
}
}
UITableView
只是向您显示数组中的相同字符串。您正在使用 append
方法,该方法总是将项目添加到数组的末尾。相反,您应该对数组进行排序,UITableView
中显示的数据将被排序。
数组有 sort
个,您为其提供了比较闭包。您可以制作一个排序方法并将其作为 isOrderedBefore
参数传递给 sort
,或者您可以这样做。
stringArray.sort(){ [=10=] < }
其中 [=17=]
和 是将要比较的第一个和第二个参数。
对于类似排序顺序的 Finder,您可以使用 localizedStandardCompare。
stringArray.sortInPlace { (x, y) -> Bool in
return x.localizedStandardCompare(y) == NSComparisonResult.OrderedAscending
}
还要添加到@hannad 的答案中,您应该使用sortInPlace 来确保对stringArray 进行排序。否则使用类似
sortedArray = stringArray.sort(){ [=11=] < }