gpt4 book ai didi

ios - Tableviewcontroller "cellForRowAtIndexPath"- 索引从 4 开始,而不是 0

转载 作者:行者123 更新时间:2023-11-29 02:01:10 26 4
gpt4 key购买 nike

我在 pageviewcontroller 上放了三个 TableView Controller 。

pageviewcontroller加载中间vc。

当我转到左侧 vc 并拉出表以重新加载时 - 我遇到问题,在函数 cellForRowAtIndexPath 上,indexPath.row 从 4 开始,而不是 0。

为什么 indexPath.row 从 4 而不是 0 开始?

我想,这不是代码问题!我有错误:

Cannot index empty buffer

我发现了错误。我不知道为什么会发生这种情况,但是当我重新加载表(拉它)时,出于某种原因,它占用了最后一行(在我的例子中,我在屏幕上有 4 行从 0 开始计数)并在 cellAtIndex 数组中使用它。每次从互联网上提取数据之前,我都会从数组中删除所有元素。我更改了该代码,我确实在重新加载之前删除了所有元素并且没有出现错误。有趣的是,我在其他 vc 上使用相同的功能并且一切正常。

我正在发出两个下载数据的请求:

class func JSONRequest2(urlInput: String, tableName: UITableView, action: (NSArray)->Void, refresh:UIRefreshControl, viewContr: UIViewController, hideLoadingViewAndStopAnimating: ()->Void) {



let urlPath = urlInput

let url = NSURL(string: urlPath)

let session = NSURLSession.sharedSession()

println("started first json request")

let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in

if error != nil {
println(error)
let stringError = error.localizedDescription
CommonFunctions.showAlert("Ошибка", alertText: stringError, alertButtonText: "Закрыть", viewController: viewContr)
}
else{

var err: NSError?
if data != nil {

if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSArray {
println("the number of news in json :\(jsonResult.count)")

if (err == nil) {

dispatch_async(dispatch_get_main_queue(), {
action(jsonResult)
//tableName.reloadData()
})
}else{
let stringError = err?.localizedDescription
CommonFunctions.showAlert("Ошибка", alertText: stringError!, alertButtonText: "Закрыть", viewController: viewContr)
}
}else {
hideLoadingViewAndStopAnimating()
println("json is not valid")
var dic = dictForErrors()
CommonFunctions.showAlert("Ошибка", alertText: dic.alertText, alertButtonText: "Закрыть", viewController: viewContr)
}
}else {

CommonFunctions.showAlert("Ошибка", alertText: "data is nil",alertButtonText: "Закрыть", viewController: viewContr)
println("json data is nil")
}
}
})

task.resume()
}

class func JSONRequest(urlInput: String, tableName: UITableView, action: (NSArray)->Void, refresh:UIRefreshControl, category: Int, viewContr: UIViewController, hideLoadingViewAndStopAnimating: ()->Void) {

//refresh.beginRefreshing()

let urlPath = urlInput

let url = NSURL(string: urlPath)

let session = NSURLSession.sharedSession()

println("started second request")

let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in

if error != nil {
println(error)
let stringError = error.localizedDescription
CommonFunctions.showAlert("Ошибка", alertText: stringError, alertButtonText: "Закрыть", viewController: viewContr)
}
else{

var err: NSError?
if data != nil{
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSArray {
println("the number of news in json :\(jsonResult.count)")

if (err == nil) {



dispatch_async(dispatch_get_main_queue(), {
action(jsonResult)
hideLoadingViewAndStopAnimating()
tableName.reloadData()
refresh.endRefreshing()
})

}else{
let stringError = err?.localizedDescription
hideLoadingViewAndStopAnimating()
CommonFunctions.showAlert("Ошибка", alertText: stringError!, alertButtonText: "Закрыть", viewController: viewContr)
}
}else {
hideLoadingViewAndStopAnimating()
println("json is not valid")
var dic = dictForErrors()
CommonFunctions.showAlert("Ошибка", alertText: dic.alertText, alertButtonText: "Закрыть", viewController: viewContr)
}
}else{

CommonFunctions.showAlert("Ошибка", alertText: "data is nil",alertButtonText: "Закрыть", viewController: viewContr)
println("json data is nil")
}
}
})

task.resume()
}

这就是我发出请求以下载两个请求的方式

self.refreshControl = self.refreshController
self.refreshControl?.addTarget(self, action: "loadDataNewsLenta", forControlEvents: .ValueChanged)


if arrayMainPage.count > 0 {
activityView.alpha = 0.0
arrayNewSLenta = arrayMainPage
self.tableView.reloadData()
}else{
// loading first time news
activityView.alpha = 0.5
activityIndicator.startAnimating()
isFirstReq = true
arrayNewSLenta.removeAll(keepCapacity: false)
CommonFunctions.JSONRequest2(urlString, tableName: tableView, action: desirializeJSONToArray, refresh: self.refreshController, viewContr: self, hidLoadingViewAndStopAnimating)
}

我把上面的代码放在viewdidload上了。这段代码是我重新加载的地方:

func hidLoadingViewAndStopAnimating() {
activityView.alpha = 0
activityIndicator.stopAnimating()
}

func configureTableView() {
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 110.0
}

func loadDataNewsLenta() {
arrayNewSLenta.removeAll(keepCapacity: false)
isFirstReq = true
refreshController.beginRefreshing()
CommonFunctions.JSONRequest2(urlString, tableName: tableView, action: desirializeJSONToArray, refresh: self.refreshController, viewContr: self, hidLoadingViewAndStopAnimating)
}

这都是关于 tableview 代码和 json 到数组的反序列化:

 // MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
println("it is news lenta count \(arrayNewSLenta.count)")
return arrayNewSLenta.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("lentaCell", forIndexPath: indexPath) as LentaTableViewCell
let inex = indexPath.row
cell.labelDateOfPublication.text = arrayNewSLenta[indexPath.row].pubDate
cell.labeltitle.text = arrayNewSLenta[indexPath.row].pageTitle

return cell
}

func desirializeJSONToArray(jsonArray: NSArray){

for singleJSON in jsonArray{
var singleArticle = ArticleInfo()

if let arrayText = singleJSON as? NSDictionary{

if let articleID = arrayText["id"] as? NSString{
singleArticle.articleID = articleID
}

if let pageTitle = arrayText["pagetitle"] as? NSString {
singleArticle.pageTitle = pageTitle
}

if let longTitle = arrayText["longtitle"] as? String{
singleArticle.longTitle = longTitle
}
if let introText = arrayText["introtext"] as? String{

singleArticle.introText = introText
}
if let contentText = arrayText["content_text"] as? String{
singleArticle.contentText = contentText

}
if let category = arrayText["category"] as? String{

singleArticle.category = category
}
if let imageLink = arrayText["thumbnail"] as? String{
singleArticle.linkToImage = imageLink

}
if let videoLink = arrayText["video"] as? String{
singleArticle.videoLink = videoLink
println("hre is video link")
println(videoLink)
}
if let sity = arrayText["sity"] as? String{

singleArticle.sity = sity
}
if let visible = arrayText["visible"] as? String{
singleArticle.visible = visible

}
if let visits = arrayText["visits"] as? String{

singleArticle.visits = visits
}
if let pubDate = arrayText["pubdate"] as? String{

singleArticle.pubDate = pubDate
}

// insert result into array
arrayNewSLenta.append(singleArticle)
}
}
//cycle ended
if isFirstReq == true {

let urlStringSecondRequest = "http://www.kfdz/artifdcles/JsonMainList"
CommonFunctions.JSONRequest(urlStringSecondRequest, tableName: tableView, action: desirializeJSONToArray, refresh: self.refreshController, category: 0, viewContr: self, hidLoadingViewAndStopAnimating)
}
isFirstReq = false
}

我注意到一个有趣的事实,当我转到第一个 vc 并立即拉出表时,出于某种原因,它仅使用屏幕上的最后一行(在我的情况下为索引 4)加载 cellAtIndex 函数。另一方面,当我去第一个 vc 并选择一些项目(查看详细 View )并返回并拉出表格时,一切正常。关于更多问题,当我将 uipageviewcontroller 的过渡样式更改为 PageCurl 时,我没有这个问题!

最佳答案

您可以不在单个 View 中使用 3 个 TableView ,而是应该在第一个 View 中使用容器 View ,并将所有 View 表放在不同的 View 中,如下图所示:

enter image description here

为每个创建的 TableView 需要 1 个文件。

bug可能就这样消失了

关于ios - Tableviewcontroller "cellForRowAtIndexPath"- 索引从 4 开始,而不是 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30371355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com