2015-10-08 22 views
0

我在UITableView的頂部獲得了空白行,並且選擇了索引不正確的行。它之前工作正常,但它沒有工作後,我改變了Xcode 7和Swift 2.0。以下是我的代碼。如果您有任何解決此問題的建議,請告訴我。UITableView中的空行在轉換swift 2.0和xcode 7的代碼之後

class DistributionAPNTableViewController: UITableViewController, UIPopoverPresentationControllerDelegate,UpdateUIDelegate { 

    var jsonReleaseNotificationArray:NSArray = [] 


    var activityView : UIActivityIndicatorView = UIActivityIndicatorView() 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     let url: String = Constants.RelNotificationsURL 
     let restCall = CallRESTParser() 
     restCall.uiDelegate=self 
     self.showProgress() 
     restCall.processRequest(url,type: Constants.RelNotificationsType) 
    } 


    func UpdateReleaseNotificationUI(jsonArray:NSArray){ 
     self.dismissProgress() 
     jsonReleaseNotificationArray = jsonArray 
     self.tableView.reloadData() 


    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     var cnt:Int = 0 
     if jsonReleaseNotificationArray.count > 0 { 
      cnt = jsonReleaseNotificationArray.count 
     } 
     else if jsonReleaseNotificationArray.count == 0{ 
      cnt = 1 
     } 
     return cnt 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cellIdentifier = "RelCell" 

     var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 
     cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, 
       reuseIdentifier: cellIdentifier) 
     if jsonReleaseNotificationArray.count > 0{ 

      let jsonResult = jsonReleaseNotificationArray[indexPath.row] as? NSDictionary 
     if let noti = jsonResult!.objectForKey("notification") as? String{ 
      cell.textLabel?.text = noti 
      cell.textLabel?.font = UIFont(name:"Avenir", size:14) 
     } 
     if let user = jsonResult!.objectForKey("userid") as? String{ 
      cell.detailTextLabel?.text = "Updated By : \(user)" 
     } 
     }else 
     { 
      cell.textLabel?.text = "There is no release notifications yet." 
      cell.textLabel?.font = UIFont(name:"Avenir", size:14) 

     } 
     return cell 
    } 
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     let jsonResult = jsonReleaseNotificationArray[indexPath.row] as? NSDictionary 
     let destination:NotificationDetails = storyboard!.instantiateViewControllerWithIdentifier("notification") as! NotificationDetails 
     if let noti = jsonResult!.objectForKey("notification") as? String{ 
      destination.messageStr = noti 
     } 
     if let user = jsonResult!.objectForKey("userid") as? String{ 
      destination.updatedByStr = user 
     } 


     navigationController?.pushViewController(destination, animated: true) 
    } 

    func UpdateProjectUI(data:NSDictionary){ 
    } 
    @available(iOS 8.0, *) 
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle 
    { 
     return UIModalPresentationStyle.None 
    } 

    func showProgress() { 

     self.view.alpha = 0.5 

     self.view.userInteractionEnabled = false 

     self.activityView.center = self.view.center 
     self.activityView.color = UIColor.blackColor() 

     self.view .addSubview(self.activityView) 

     self.activityView.startAnimating() 
    } 


    func dismissProgress() { 

     self.activityView.stopAnimating() 

     self.activityView.removeFromSuperview() 

     self.view.alpha = 1.0 

     self.view.userInteractionEnabled = true 
    } 
    func UpdateRelNotiByMsgUI(data:NSDictionary){ 
    } 
    func ErrorHasOccured(error:String){ 
     self.dismissProgress() 
     if #available(iOS 8.0, *) { 
      let alert = UIAlertController(title: "IBM Vidur", message: error, preferredStyle: UIAlertControllerStyle.Alert) 
      alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
      self.presentViewController(alert, animated: true, completion: nil) 
     } else { 
      let alertController: UIAlertView = UIAlertView(title: "IBM Vidur", message:error, delegate: nil, cancelButtonTitle: "OK") 
      alertController.show() 
     } 
    } 

} 

回答

1
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 

cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, 
       reuseIdentifier: cellIdentifier) 

這兩條線可以做一些混亂。你正在離隊,而不是你剛開始另一個。

我想,你應該只使用第一行來創建tableView單元格。該單元格應該在tableView中正確設置爲原型單元格。在我的UITableView的頂部

+0

好的,我們如何設置它在單行?我也需要字幕。 – RichieRich

+0

在故事板中設計您的tableViewCell,以獲得所需的標籤。 – dirtydanee

1

空行也選擇該行的 指數不來正確

這清楚地表明你的cellForRowAtIndexPath功能,對於一些細胞,textLabel & detailTextLabel是根本不設置,在這種情況下單元格將顯示爲空。您可以通過點擊第一個單元格並在didSelectRowAtIndexPath函數中打印單元格索引來驗證這一點。

我建議你在cellForRowAtIndexPath函數中設置一個斷點,並檢查每個空行索引單元格上設置的值。

相關問題