2016-09-29 126 views
1

有沒有辦法讓UITableView單元格的負載被正確地滑動以顯示編輯功能的存在。我想這樣做是爲了教會我的應用程序中的用法,它可以在具有該功能的一個tableView中完成。自動刷新UITableVIew單元格(編輯)

例如我已附加了一個短小的電子郵件應用程序的屏幕,並希望我的應用程序能夠加載已經這樣刷過的單元格。

enter image description here

在此先感謝。 )

的AnimateCell的ObjectiveC回答以下

變種細胞的迅速轉化率:的UITableViewCell?

FUNC animateCell(){

let indexPath = NSIndexPath(row: 2, section: 1) 



cell = self.tableView.cellForRow(at: (indexPath as NSIndexPath) as IndexPath) 

// The crash occurs here with lldb 
var aLabel = UILabel(frame: CGRect(x: (cell?.bounds.size.width)!, y: 0, width: 200, height: (cell?.bounds.height)!)) 
aLabel.text! = "Delete" 
aLabel.backgroundColor = UIColor.red 
aLabel.textColor = UIColor.white 
cell?.addSubview(aLabel) 

UIView.animate(withDuration: 0.3, animations: {() -> Void in 
    self.cell?.frame = CGRect(x: (self.cell?.frame.origin.x)! - 100, y: (self.cell?.frame.origin.y)!, width: (self.cell?.bounds.size.width)!, height: (self.cell?.bounds.size.height)!) 

    }, completion: {(finished: Bool) -> Void in 
     UIView.animate(withDuration: 0.5, animations: {() -> Void in 

      self.cell?.frame = CGRect(x: (self.cell?.frame.origin.x)! + 100, y: (self.cell?.frame.origin.y)!, width: (self.cell?.bounds.size.width)!, height: (self.cell?.bounds.size.height)!) 

      }, completion: {(finished: Bool) -> Void in 
       aLabel.removeFromSuperview() 
       aLabel = nil 
     }) 
}) 

}

+0

有沒有很多混凝土林ks在線。我知道如何構建編輯過程以幫助用戶輕掃和刪除單元及其數據。但是如何實現自動化卻沒有明確的方法。我目前的方法是使用自定義彈出窗口來告訴用戶有關此功能的信息。 – Gugulethu

回答

0

你要設置默認編輯模式下的tableview。編輯模式是UITableView的一種狀態。您可以通過編程方式將其設置爲:

[tableView setEditing:YES animated:YES] 

您可以使用或不使用動畫。

+0

不幸的是,這種方法只會奇怪地顯示左邊的刪除按鈕。我當然認爲它會起作用。 – Gugulethu

1

你可以嘗試這樣的事情:

-(void)animateCell{ 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

    __block UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.bounds.size.width, 
                      0, 
                      200, 
                      cell.bounds.size.height)]; 

    aLabel.text = @"Delete"; 
    aLabel.backgroundColor = [UIColor redColor]; 
    aLabel.textColor = [UIColor whiteColor]; 
    [cell addSubview:aLabel]; 

    [UIView animateWithDuration:0.3 animations:^{ 
     [cell setFrame:CGRectMake(cell.frame.origin.x - 100, cell.frame.origin.y, cell.bounds.size.width, cell.bounds.size.height)]; 
    } completion:^(BOOL finished) { 
     [UIView animateWithDuration:0.5 animations:^{ 
      [cell setFrame:CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y, cell.bounds.size.width, cell.bounds.size.height)]; 
     } completion:^(BOOL finished) { 
      [aLabel removeFromSuperview]; 
      aLabel = nil; 
     }]; 
    }]; 

} 

您可以在很短的時間之後,調用此方法viewDidAppear ..

UPDATE

對於斯威夫特3,你可以使用類似的參考:

func animateCell() { 
     var cell : UITableViewCell? 
     let indexPath = NSIndexPath(row: 1, section: 0) 
     cell = self.tableView.cellForRow(at: (indexPath as NSIndexPath) as IndexPath) 

     var aLabel : UILabel! = UILabel() 
     //aLabel.translatesAutoresizingMaskIntoConstraints = false 
     aLabel.frame = CGRect(x: (cell?.bounds.size.width)!, y: 0, width: 400, height: (cell?.bounds.height)!) 
     cell?.clipsToBounds = false 
     print(aLabel.frame) 
     aLabel!.text = "Delete" 
     aLabel!.backgroundColor = UIColor.red 
     print(aLabel.frame) 
     aLabel.textColor = UIColor.white 
     cell?.addSubview(aLabel) 


     UIView.animate(withDuration: 0.5, animations: { 
      cell?.frame = CGRect(x: (cell?.frame.origin.x)! - 100, y: (cell?.frame.origin.y)!, width: (cell?.bounds.size.width)!, height: (cell?.bounds.size.height)!) 

      }) { (finished: Bool) in 
       UIView.animate(withDuration: 0.5, animations: { 
        cell?.frame = CGRect(x: (cell?.frame.origin.x)! + 100, y: (cell?.frame.origin.y)!, width: (cell?.bounds.size.width)!, height: (cell?.bounds.size.height)!) 

        }, completion: { (finished: Bool) in 
         aLabel.removeFromSuperview() 
         aLabel = nil 
       }) 
     } 
    } 
+0

我很快就重寫了這個,但沒有取得太大的成功。我發佈了我的迅速重寫並評論了發生崩潰的地點。你介意給我一個我可以做的事情的小費。 – Gugulethu