你好我试图完成我的任务,因为2天但仍然没有成功搜索到处,我想从tableview单元格中选择多行(包含2个标签和一个图像)然后我想转移到另一个vc并在tableview中显示,我能够选择多行并从选定的行中获取此类型索引,但现在我不知道如何获取数据并转移到另一个vc并在表视图中显示请帮助我获取选择的行索引,如此[[0,1] ],[0,2],[0,3]
VS代码
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var labone = ["1","2","3","4","5","6"]
var labtwo = ["a","b","c","d","e","f"]
var img = ["bag","bag","bag","bag","bag","bag"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func button(_ sender: Any) {
let selectedindexPath = tableview.indexPathsForSelectedRows
if(selectedindexPath != nil){
let toy = labone[0]
print(toy)
print(selectedindexPath) }
else{
print("null")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return labone.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let name = cell.viewWithTag(1) as! UILabel
let name_two = cell.viewWithTag(2) as! UILabel
let imgg = cell.viewWithTag(3) as! UIImageView
name.text = labone[indexPath.row]
name_two.text = labtwo[indexPath.row]
imgg.image = UIImage(named: img[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
cell.contentView.backgroundColor = UIColor.yellow
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
// let selectedCell = tableview.cellForRow(at: indexPath)
if let label = cell?.contentView.viewWithTag(4) as? UIImageView {
label.image = UIImage(named: "check_disable")
}
}
}
您应该使用如下所示的结构,然后更新所选项目或取消选择的项目状态,然后当您想要转到下一个viewController时,您可以使用下面选择的项目过滤dataSource,
class CellModel {
var labelOne: String
var labelTwo: String
var imageName: String
var isSelected = false
init(_ labelOne: String, labelTwo: String, imageName: String) {
self.labelOne = labelOne
self.labelTwo = labelTwo
self.imageName = imageName
}
}
更新你的viewController,
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
let dataSource = [CellModel("1", labelTwo: "a", imageName: "bag"),
CellModel("2", labelTwo: "b", imageName: "bag"),
CellModel("3", labelTwo: "c", imageName: "bag"),
CellModel("4", labelTwo: "d", imageName: "bag"),
CellModel("5", labelTwo: "e", imageName: "bag"),
CellModel("6", labelTwo: "f", imageName: "bag")]
}
然后你可以更新你的cellForRowAt,如下所示,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let name = cell.viewWithTag(1) as! UILabel
let name_two = cell.viewWithTag(2) as! UILabel
let imgg = cell.viewWithTag(3) as! UIImageView
let model = self.dataSource[indexPath.row]
name.text = model.labelOne
name_two.text = model.labelTwo
imgg.image = UIImage(named: model.imageName)
return cell
}
用这个更新numberOfRowInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count
}
并且你可以像这样选择/取消选择单元格时更新模型的状态,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
cell.contentView.backgroundColor = UIColor.yellow
self.dataSource[indexPath.row].isSelected = true
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
// let selectedCell = tableview.cellForRow(at: indexPath)
if let label = cell?.contentView.viewWithTag(4) as? UIImageView {
label.image = UIImage(named: "check_disable")
self.dataSource[indexPath.row].isSelected = false
}
}
最后在按钮操作中,您可以过滤所选模型并传递给下一个viewController
@IBAction func button(_ sender: Any) {
let selectedItems = self.dataSource.filter({ $0.isSelected == true })
// Pass to next ViewController the selectedItmes
}
正如@Rakshith在他们的评论中所建议的那样,将您的数据(labone,labtwo,img数组)移出视图控制器并移动到模型对象中。将该模型对象传递给第二个视图控制器。
还要在第二个视图控制器中创建一个数组selectedIndexPaths
。当您点击ViewController
中的按钮时,获取所选索引路径的数组并将其传递给第二个视图控制器。
在第二个视图控制器的viewWillAppear
中,使用selectedIndexPaths
变量将所选项目复制到数组itemsToDisplay中,并在表视图数据源方法中使用它来填充第二个视图控制器的表视图。