如果我修改表格视图中的一行,它也会修改其他一些单元格.(环球银行间金融电信协会编程)



我在UITableView中使用可重用的标识符创建了自定义单元格。因此,每当我删除该单元格或更改该单元格的颜色时,其他一些单元格以及该单元格也会修改。

实际上,我只想修改或删除一个单元格。

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

TableViewCell 在向上或向下滚动时重复使用。您应该更改数据源,然后重新加载表或单元格,在函数cellForRowAt 中配置单元格

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

最新更新