2017-02-10 29 views
1

我在UITableView中創建了具有可重複使用標識符的自定義單元。因此,無論何時刪除該單元或更改顏色(如果該單元),此單元的其他一些單元也會被修改。如果我修改表格視圖中的一行,並修改其他一些單元格。 (SWIFT編程)

其實我只想要修改一個單元格或刪除它。

import UIKit 
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    var itemName = ["ABCD", "EFGH", "IJK", "LMNO", "PQRST", "abcd", "efgh", "ijk", "lmno", "pqrst"] 

    //Group List. 
    var arrGroups = [GROUPLIST](repeating: GROUPLIST(), count: 100) 

    var nCount = 5 

    var nMaxCount = 100 

    var curItemName:String = "" 

    @IBOutlet weak var tableView: UITableView! 

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     //Return the row count of the table. 
     return nCount 
    } 


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

     arrGroups[indexPath.row].strGroupName = itemName[indexPath.row % itemName.count] as NSString 

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

     //Select the image for the image view. 
     cell.myImage.image = UIImage(named: "download.jpeg") 

     //Convert the image into oval shape. 
     cell.myImage.layer.cornerRadius = cell.myImage.frame.size.width/2 

     //Clip the image according to the bound value. 
     cell.myImage.clipsToBounds = true 



     //Select the text to display in the label view. 
     cell.myLabel.text = String(arrGroups[indexPath.row].strGroupName) 

     cell.myLabel.adjustsFontSizeToFitWidth = true 
     cell.myLabel.minimumScaleFactor = 0.5 

     cell.mySubLabel.text = "SubTitle" 
     cell.mySubLabel.adjustsFontSizeToFitWidth = true 

     //To prefetch the data 
     if(nCount - 5 == indexPath.row) 
     { 
      if(nCount < nMaxCount) 
      { 
       //Load more. 
       nCount += 10 

       tableView.reloadData() 
      } 
     } 

     return(cell) 
    } 


    //To set the row height. 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     //Get the main screen size. 
     let screenSize: CGRect = UIScreen.main.bounds 
     return screenSize.height/5 
    } 

    //Return true to edit/delete the specified row. 
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
     return true 
    } 


    //Remove button press action. 
    @IBAction func buttonRemove(_ sender: UIButton) { 


     let point = sender.convert(CGPoint.zero, to: self.tableView) 

     let indexPath = self.tableView.indexPathForRow(at: point) 


     let cell = tableView.cellForRow(at: indexPath!) as! ViewControllerTableViewCell 

     cell.buttonRemove.setImage((UIImage(named: "download.jpeg")), for: UIControlState.normal) 


    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
      // Do any additional setup after loading the view, typically from a nib. 
    } 

} 

class GROUPLIST : NSObject{ 
    //Group name. 
    var strGroupName = NSString() 
    //Member count. 
    var nMember = NSInteger() 
    //Group Image. 
    var strURL = NSString() 
} 

回答

2

當向上或向下滾動時,TableViewCell被重用。你應該改變的數據源,然後重新裝入表或單元格,在功能cellForRowAt Config中的細胞

tableView.reloadData() 
tableView.reloadRows(at: [indexPath], with: .none) 
tableView.deleteRows(at: [indexPath], with: .automatic) 

更新:

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    //Return the row count of the table. 
    return itemName.count 
} 


//Remove button press action. 
@IBAction func buttonRemove(_ sender: UIButton) { 
    let point = sender.convert(CGPoint.zero, to: self.tableView) 
    if let indexPath = self.tableView.indexPathForRow(at: point) { 
     itemName.remove(at: indexPath.row) //change data model 
     tableView.deleteRows(at: [indexPath], with: .automatic) //update cell 
    } 
} 
+0

如何更改數據源&u能請告訴我在哪裏添加上述3代碼行。 –

+0

我想更改removeButton的圖像。所以每當我改變那個時候,一些其他的行圖像也在變化。 –

+0

'@IBAction FUNC buttonRemove(_發件人:的UIButton){ 設點= sender.convert(CGPoint.zero,到:self.tableView) 如果讓indexPath = self.tableView.indexPathForRow(在:點){ 設cell = tableView.cellForRow(at:indexPath)as! ViewControllerTableViewCell cell.buttonRemove.setImage((的UIImage(命名爲: 「download.jpeg」)),用於:UIControlState.normal) tableView.reloadRows(在:[indexPath],其中:.none) } }' –

相關問題