2016-12-02 166 views
0

粉紅色被賦予單元格的內容視圖。單元格下移到右側單元格滑動刪除UITableView

雖然通過刷卡刪除電池使用委託commitEditingStyle

enter image description here

刪除僅刪除刪除單元格下方的單元之後被轉移到右側

我沒有得到任何有關的想法那爲細胞留下了餘量。 滾動並返回到放錯位置的單元格時,佈局正確並且該邊距被移除。

我在Xcode 8.0中使用了Swift。

下面是代碼:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if (editingStyle == UITableViewCellEditingStyle.Delete) { 
      // handle delete (by removing the data from your array and updating the tableview) 
      let date = dateKeys[indexPath.section] 
      var notificationObject = dataSection[date] as! [Notification] 
      let deleteNotificationId = notificationObject[indexPath.row].notifId 

      for notifObject in self.notificationArray { 
       if notifObject.notifId == deleteNotificationId { 
        if let index = self.notificationArray.indexOf(notifObject) { 
         self.notificationArray.removeAtIndex(index) 
         self.dataSection = self.categorizeNotification(self.notificationArray) 
         self.dateKeys = self.sortArrayDate() 
         self.notificationTable.reloadData() 
         break 
        } 
       } 
      } 
     } 
    } 
+0

我在刪除「commitEditingStyle」函數中的數組中的項後重新加載表。 –

+0

在這裏發佈您的commitEditingStyle代碼。 – KAR

+0

在此處提及帶有屏幕截圖的代碼。 – KKRocks

回答

0

你應該寫爲刪除此行代碼表視圖:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    // the cells you would like the actions to appear needs to be editable 
    return true 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) 
{ 
    if editingStyle == .delete 
    { 
     showsArray.remove(at: indexPath.row) 
     tableView.reloadData() 
    } 
} 
+0

是的,它已經在我的代碼中。刪除操作是通過從數組中刪除並重新加載從數組填充的數據是正確的,但刪除的單元格正下方的單元格移動到右側。 然後在滾動表格之後,當該單元格再次出現時,它會固定在正確的位置。 –

+0

請勿重新載入您的表格。只需從數組中刪除對象。 –

+0

抱歉它沒有工作... –

0

Occour同樣的問題。

override func layoutSubviews() { 
     super.layoutSubviews() 
     if self.editingStyle != .delete && self.frame.origin.x > 0{ 
      var originFrame = self.frame 
      originFrame.origin.x = 0 
      self.frame = originFrame 
     } 
    } 

設置的UITableViewCell frame.origin.x零避免它。

相關問題