【发布时间】:2015-09-16 15:14:54
【问题描述】:
我知道有数以千计的 tableView.reloadData() 问题有答案,但没有一个对我有用!有人可以告诉我如何正确重新加载数据吗?很难解释我的问题,所以我将向您展示我的代码。 这是我的整个文件:
import UIKit
import Foundation
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var amountField: UITextField!
@IBOutlet weak var whoPaysLabel: UILabel!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var datePicker: UIDatePicker!
var names:NSMutableArray = []
var amounts:NSMutableArray = []
var dates:NSMutableArray = []
var whoPays:NSMutableArray = []
var status:NSMutableArray = []
var arrayNumber:Int = -1
@IBOutlet weak var tableView: UITableView!
let textCellIdentifier = "EntityCell"
var entityCellArray:[String] = []
let newDate = NSDate()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func changeWhoPays(sender: UISwitch) {
if(sender.on){
whoPaysLabel.text = "\(nameField.text) pays you"
}else{
whoPaysLabel.text = "you pay \(nameField.text)"
}
}
@IBAction func changeStatus(sender: UISwitch) {
if sender.on{
statusLabel.text = "paid"
}else{
statusLabel.text = "not paid"
}
}
func createNewEntity(){
names.addObject(nameField.text)
if let amountField_ = amountField.text.toInt() {
amounts.addObject(amountField_)
}
dates.addObject(datePicker.date)
if whoPaysLabel.text == "\(nameField.text) pays you"{
whoPays.addObject("they pay")
}else{
whoPays.addObject("you pay")
}
status.addObject(statusLabel.text!)
arrayNumber++
entityCellArray.append("\(names[arrayNumber) - \(amounts[arrayNumber]) - \(dates[arrayNumber]) - \(status[arrayNumber])")
let created = UIAlertController(title: "success", message: "creation successful", preferredStyle: UIAlertControllerStyle.Alert)
created.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil))
presentViewController(created, animated: true, completion: nil)
nameField.text = ""
amountField.text = " "
whoPaysLabel.text = "..."
datePicker.date = newDate
refreshTableView()
}
//test function, not to be implemented in app
func printCount(){
println(entityCellArray.count)
}
func refreshTableView(){
self.tableView?.reloadData()
}
@IBAction func createButtonPressed(sender: UIButton){
createNewEntity()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return entityCellArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = entityCellArray[indexPath.row]
return cell
}
}
旁注:我是 swift 新手,我很肯定我的 tableView dataSource 和 delegate 设置正确。希望有人能指出我做了什么来得到这个错误:[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
【问题讨论】:
-
您的
tableView插座连接了吗?
标签: ios arrays swift uitableview reload