且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将UIImageView更改为自定义单元格

更新时间:2023-02-13 17:33:43

在您的表视图上,将多个选择设置为NO:

On your tableview set multiple selection to NO:

tableView.allowsMultipleSelection = false

然后在TableViewController上想要这样的东西

Then you want something like this on your TableViewController

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

   // Store which indexPath has been selected in a global variable

   tableView.reloadData()
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.reloadData()
}

在cellForRow方法中,您想要类似...

In your cellForRow method you want something like...

cell.useSelectedImage(indexPath == storedIndexPath)

最后在您的自定义单元格类中哟您想添加一个与此功能相似的方法

And finally in your custom cell class you want to add a method that does similar to this

func useSelectedImage(bool:useSelected)
{
     self.myImage = useSelected ? "selectedImage" : "unselectedImage"
}

我的Swift有点生锈...但您应该可以从我在这里所做的事情中获得大致的认识

My Swift is a bit rusty... but you should be able to get the general idea from what I've done here