• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

使用swift将日期显示到tableview单元格时出现索引超出范围3

IOS 来源:silvervein 4次浏览

大家好我在我的tableview中有一个自定义UITableViewCell,带有2个标签和一个imageview。在第一个标签中,我获取通知消息的数组,它将在中运行正常第二个标签m在此情况下获取日期数组m获取错误消息索引超出范围。任何人都可以帮助我如何解决它。推进附使用swift将日期显示到tableview单元格时出现索引超出范围3

是JSON响应这里:

{ 
    "Notification_Details": 
    [ 
     { 
      "student_id": 2, 
      "date": "2017-06-03T00:00:00", 
      "message": "Notification Message 3" 
     }, 
     { 
      "student_id": 2, 
      "date": "2017-06-02T00:00:00", 
      "message": "Notification Message 2" 
     }, 
     { 
      "student_id": 2, 
      "date": "2017-06-01T00:00:00", 
      "message": "Notification Message 1" 
     } 
    ] 
} 

和下面是使用来获取JSON和装入的tableview细胞代码米。

import UIKit 

class NotificationsViewController: UIViewController,UITableViewDataSource,UITableViewDelegate 
{ 
@IBOutlet weak var mytable: UITableView! 

var notilabell : Array = [String]() 
var datelabel : Array = [String]() 


override func viewDidLoad() { 
    super.viewDidLoad() 

    self.getnotiJSON() 

} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return notilabell.count 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! NotiCellTableViewCell 

      cell.noti.text = notilabell[indexPath.row] 
    cell.datenotification.text = datelabel[indexPath.row] 



    return cell 

} 

func getnotiJSON() 
{ 
     let url = NSURL(string: "http://ezschoolportalapi.azurewebsites.net/api/Student/NotificationDetails?schoolid=1&studentid=2") 
    let request = NSMutableURLRequest(url: url! as URL) 
    let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) 
     in 
     guard error == nil && data != nil else 
     { 
      print("Error:",error) 
      return 
     } 
     let httpstatus = response as? HTTPURLResponse 
     if httpstatus?.statusCode == 200 
     { 
      if data?.count != 0 
      { 
      let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary 

        let notiarray = responseString?.value(forKey: "Notification_Details") as? NSArray 

         for notifi in notiarray! 
         { 
          let notidict = notifi as? NSDictionary 

           let notiname = notidict?.value(forKey: "message") 

            self.notilabell.append(notiname as! String) 

          let notidat = notidict?.value(forKey: "message") as! String 
          let dateformatter = DateFormatter() 
          dateformatter.dateFormat = "yyyy-MM-dd hh:mm:ss" 
          let mystring = dateformatter.string(from: Date()) 
          let yourdate = dateformatter.date(from: mystring) 
          dateformatter.dateFormat = "dd-MM-yyyy" 
          let mynewdate = dateformatter.string(from: yourdate!) 
            self.notilabell.append(mynewdate as! String) 

         } 

       DispatchQueue.main.async         { 
        self.mytable.reloadData() 
        } 
      } 
      else 
      { 
       print("No data got from URL") 
      } 
     } 
     else{ 
      print("error httpstatus code is :",httpstatus?.statusCode) 
     } 

    } 
    task.resume() 
    } 

} 

===========解决方案如下:

错误与您在解析日期时使用的日期格式化程序有关。你的日期也是错误的。尝试使用

let notidat = notidict?.value(forKey: "date") as! String 
    let dateformatter = DateFormatter() 
    dateformatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ss" 

要附加日期notilabell阵列和下面的代码

let mynewdate = dateformatter.string(from: yourdate!) 
self.notilabell.append(mynewdate as! String) 

datelabel阵列 更改为

let mynewdate = dateformatter.string(from: yourdate!) 
self.datelabel.append(mynewdate as! String) 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)