ios - UITableView 错误 : indexpath. row is starting from 1 instead of 0

标签 ios swift uitableview

indexpath.row 从 1 而不是 0 开始。

我怎样才能摆脱空索引 0?

import UIKit

class ViewController: UITableViewController {

var path: String?
var data: String?
var TableView1 = [String]()
var TableView2 = [String]()


override func viewDidLoad() {
    super.viewDidLoad()

    self.path = NSBundle.mainBundle().pathForResource("textFile", ofType: "txt")
    self.data = String(contentsOfFile:path!, encoding: NSUTF8StringEncoding, error: nil)
    if var content = (data){
        //var line: [String] = content.componentsSeparatedByString("\n")
        var chp: [String] = content.componentsSeparatedByString("#")

        TableView1 += chp
        TableView2 += sec
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return TableView1.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    Cell.textLabel?.text = TableView1[indexPath.row]

    return Cell
}
}

这是文本文件读取的内容以及它如何在 tableview 上显示的屏幕截图

enter image description here

enter image description here

最佳答案

这种行为是正常的,从componentsSeparatedByString的文档中看NSString类:

Discussion.
The substrings in the array appear in the order they did in the receiver. Adjacent occurrences of the separator string produce empty strings in the result. Similarly, if the string begins or ends with the separator, the first or last substring, respectively, is empty. For example, this code fragment:

NSString *list = @"Karin, Carrie, David"; NSArray *listItems = [list componentsSeparatedByString:@", "];.
produces an array { @"Karin", @"Carrie", @"David"" }.

If list begins with a comma and space—for example, @", Norman, Stanley, Fletcher"—the array has these contents: { @"", @"Norman", @"Stanley", @"Fletcher" }

If list has no separators—for example, "Karin"—the array contains the string itself, in this case { @"Karin" }.

您的文本文件以分隔符开头,因此空字符串是数组的第一项。


编辑:

一个解决方案就是删除第一项:

class ViewController: UITableViewController {

  var tableData = [String]()

  override func viewDidLoad() {
    super.viewDidLoad()

    if let path = NSBundle.mainBundle().pathForResource("textFile", ofType: "txt"),
      data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil) {
        tableData = data.componentsSeparatedByString("#")
        tableData.removeAtIndex(0)
    }
  }
}
...

关于ios - UITableView 错误 : indexpath. row is starting from 1 instead of 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32369748/

相关文章:

当特定的 unicode 时,iOS UIButton 不显示标题属性

ios - 如果在 Swift 中使用 Router 和 MVVM,谁负责配置演示文稿的具体细节?

iOS Swift webview 重新加载动态构建网站的实际站点

ios - 如何在 uitableview 的 cellForRowAt 函数中按标题日期对响应进行排序

swift - TableView 中的滚动委托(delegate)

ios - 为什么iOS应用加入团队后无法推送到App Store?

ios - 如何禁用标准的 UIButton 触摸事件

ios - RKRequestDelegate TotalBytesExpectedToReceive 始终返回 -1

ios - 在 iOS Swift 中解析 JSON 格式的最佳方法是什么?

iphone - 代表 uitableview 中的必填字段