私には問題があります。私はiPhoneアプリプログラミングとSwiftを初めて使っているので分かりません。Swiftのテーブルセルから読み込もうとしたときにゼロが見つかりました
私はいくつかの結果を表示するために使用するSwiftのTableViewを持っています。各セルにボタンを追加して、ユーザーが異なるセルを選択してから、削除を押してセルに表示されている結果を削除できるようにしました。
うまくいきましたが、今ではnil-exceptionを得るようになりました。
特定の行のセルを取得しようとすると、プログラムはgetIndexToDelete関数でクラッシュします。ここで
は、私はテーブルを扱うコードです:私はもちろんのif節内let cell = tableView.cellForRowAtIndexPath(indexPath) as! ResultTableCell
を置くことができます
import UIKit
class ResultTableCell: UITableViewCell {
@IBOutlet weak var ResultField: UITextField!
@IBOutlet weak var CheckBoxButton: UIButton!
}
が、その後、他の奇妙なこと:ここで
import UIKit
class DailyResultTable: UIViewController, UITableViewDataSource, UITableViewDelegate {
var results = Results()
var yearShownIndex = 0
var monthShownIndex = 0
var dayShownIndex = 0
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var DeleteButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// self.view.backgroundColor = UIColor(patternImage: UIImage(named: "StatisticBackground")!)
}
// DataSource
func numberOfSectionsInTableView(tableview: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return (self.results?.getDayList(yearShownIndex, posMonth: monthShownIndex).count)!
print("return number of rows")
if (results.getYearList().count > 0){
if (results.getMonthList(yearShownIndex).count > 0){
if (results.getDayList(yearShownIndex, posMonth: monthShownIndex).count > dayShownIndex){
return (results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results.count)
}
}
}
print("No numbers to show return 0")
return 0;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("DayResultCell", forIndexPath: indexPath) as! ResultTableCell
let row = indexPath.row
//cell.ResultField.text = String((results?.getDayList(yearShownIndex, posMonth: monthShownIndex)[row].day)!) + "/" + String((results?.getMonthList(yearShownIndex)[monthShownIndex].month)!)
let res = results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results[row].result
let maxRes = results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results[row].maxresult
let discipline = results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results[row].discipline
let text1 = String(res) + "/"
let text2 = String(maxRes)
let text3 = " - " + discipline
let text = text1 + text2 + text3
print(text)
cell.ResultField.text = text
return cell
}
// Delegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
@IBAction func CheckBox(sender: UIButton) {
let image = UIImage(named: "Selected") as UIImage!
let selected = sender.selected
sender.selected = !selected
sender.setImage(image, forState: .Selected)
}
func getIndexToDelete()->[Int]{
var indices = [Int]()
for i in 0..<tableView.numberOfRowsInSection(0){
let indexPath = NSIndexPath(forRow: i, inSection: 0)
// Here does the program crash
let cell = tableView.cellForRowAtIndexPath(indexPath) as! ResultTableCell
if (cell.CheckBoxButton.selected){
indices += [i]
}
}
return indices
}
@IBAction func DeletePressed(sender: UIButton) {
let deleteIndices = getIndexToDelete()
var goback = false;
var count = 0;
for index in deleteIndices{
print("Count: " + String(count))
results.ListResults[yearShownIndex].months[monthShownIndex].day[dayShownIndex].results.removeAtIndex(index-count)
count += 1
print(String((results.getDayList(yearShownIndex, posMonth: monthShownIndex)[dayShownIndex].results.count)));
}
loadView()
results.recreatePlainResult()
results.saveResults()
if (results.ListResults[yearShownIndex].months[monthShownIndex].day[dayShownIndex].results.count == 0){
print(String(results.ListResults[yearShownIndex].months[monthShownIndex].day.count))
results.ListResults[yearShownIndex].months[monthShownIndex].day.removeAtIndex(dayShownIndex)
results.recreatePlainResult()
results.saveResults()
print(String(results.ListResults[yearShownIndex].months[monthShownIndex].day.count))
goback = true
}
if (results.ListResults[yearShownIndex].months[monthShownIndex].day.count == 0){
results.ListResults[yearShownIndex].months.removeAtIndex(monthShownIndex)
results.recreatePlainResult()
results.saveResults()
goback = true
}
if (results.ListResults[yearShownIndex].months.count == 0){
results.ListResults.removeAtIndex(monthShownIndex)
results.recreatePlainResult()
results.saveResults()
goback = true
}
if (goback){
// return;
navigationController?.popViewControllerAnimated(true)
}
}
}
はResultTableCellです起こる
もしあなたがテーブルビューで押されたボタンを決定したいのであれば、button.tag = indexPath.rowを設定することをお勧めします...この方法では、この恐ろしいことなしに押したボタンを正確に知ることができます。 cellForRowAtIndexPath –