Krishna Kirana
Krishna Kirana

Reputation: 478

If I modify one row in table view, along with that it modifies some other cell also. (SWIFT programming)

I have created custom cells in UITableView with reusable identifiers.So whenever I delete that cell or change the colour if that cell, Some other cells along with this cell also modified.

Actually, I want only one cell to be modified or to be deleted.

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()
}

Upvotes: 1

Views: 487

Answers (1)

jia ma
jia ma

Reputation: 198

TableViewCell is reused when scroll up or down. You should change the datasource, and then reload table or cell, config the cell in function cellForRowAt

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

update:

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
    }
}

Upvotes: 2

Related Questions