У меня есть таблица, которая используется как для результата поиска, так и для всего списка.Я использую imageView в ячейке просмотра таблицы, которая при щелчке изменится на отметку.Я выполнил функцию щелчка и отфильтровал все, но не смог показать метку после того, как пользователь щелкнул ячейку таблицы в результатах поиска, и мне нужно заполнить эту метку во всем списке меток.Я приложил код для вашей справки.
Это функция моей ячейки
class playlistcelllaist : UITableViewCell {
@IBOutlet weak var img_Show: UIImageView!
@IBOutlet weak var lbl_Title: UILabel!
@IBOutlet weak var lbl_SubTitle: UILabel!
@IBOutlet weak var img_play: UIImageView!
@IBOutlet weak var image_tick: UIImageView!
@IBOutlet weak var cellselectionButton: UIButton!
@IBOutlet weak var trackidtext: UILabel!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
}
}
@IBAction func cellselectionButtonAction(_ sender: UIButton) {
if constantsList.playTypeArray.filtereArray.count == 0 {
//check
if image_tick.image == UIImage(named:"round_done-1") {
constantsList.playTypeArray.boolArrayAddplaylist[sender.tag] = false
let dataToberemoved = ((constantsList.playTypeArray.arr_subCatagriesLecture.object(at: sender.tag)as AnyObject).value(forKey: "track_id")!)
constantsList.playTypeArray.dataArrayInPlaylist.removeObject(identicalTo: dataToberemoved)
print(constantsList.playTypeArray.dataArrayInPlaylist)
image_tick.image = UIImage(named:"round")
//uncheck
}else if image_tick.image == UIImage(named:"round") {
constantsList.playTypeArray.boolArrayAddplaylist[sender.tag] = true
constantsList.playTypeArray.dataArrayInPlaylist.add((((constantsList.playTypeArray.arr_subCatagriesLecture.object(at: sender.tag)as AnyObject).value(forKey: "track_id")))!)
print(constantsList.playTypeArray.dataArrayInPlaylist)
image_tick.image = UIImage(named:"round_done-1")
}
if constantsList.playTypeArray.dataArrayInPlaylist.count == 0 {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "navigationBarnameChange"), object: nil)
}else {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "navigationBarnameChange"), object: nil)
}
}else {
print(sender.tag)
//check
if image_tick.image == UIImage(named:"round_done-1") {
let datainfilteredArray = constantsList.playTypeArray.filtereArray[sender.tag]
constantsList.playTypeArray.dataArrayInPlaylist.removeObject(identicalTo: datainfilteredArray.track_id)
print(constantsList.playTypeArray.dataArrayInPlaylist)
image_tick.image = UIImage(named:"round")
//uncheck
}else if image_tick.image == UIImage(named:"round") {
let datainfilteredArray11 = constantsList.playTypeArray.filtereArray[sender.tag]
constantsList.playTypeArray.dataArrayInPlaylist.add(datainfilteredArray11.track_id)
print(constantsList.playTypeArray.dataArrayInPlaylist)
image_tick.image = UIImage(named:"round_done-1")
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchBool == false {
return searchResults.count
}else {
return constantsList.playTypeArray.filtereArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let table_cell = tableView.dequeueReusableCell(withIdentifier: "playlistaddition", for: indexPath) as! playlistcelllaist
// Filtered Array
if searchBool == true {
let trackDataFilter = constantsList.playTypeArray.filtereArray[indexPath.row]
//classname
let x : Int = trackDataFilter.classname
let className = String(x)
//Play Image
table_cell.img_play.image=#imageLiteral(resourceName: "play_track")
//title
table_cell.lbl_Title.text = trackDataFilter.title
//Subtitle
table_cell.lbl_SubTitle.text = String(format: "Class - %@ | %@", className,(trackDataFilter.time))
//Color
table_cell.lbl_SubTitle.textColor = UIColor.lightGray
//Setting trackId to hidden label
table_cell.trackidtext.text = "\(trackDataFilter.track_id)"
//image
// constantsList.playTypeArray.imageLecture = trackDataFilter.imageUrl
table_cell.img_Show.sd_setImage(with: trackDataFilter.imageUrl )
table_cell.selectionStyle = .none
if constantsList.playTypeArray.dataArrayInPlaylist.count != 0 {
for travkID in constantsList.playTypeArray.dataArrayInPlaylist {
print(travkID)
if travkID as! Int == trackDataFilter.track_id {
table_cell.image_tick.image = UIImage(named:"round_done-1")
}else {
table_cell.image_tick.image = UIImage(named:"round")
}
}
}else {
table_cell.image_tick.image = UIImage(named:"round")
}
return table_cell
}
let trackdata = searchResults[indexPath.row]
constantsList.playTypeArray.boolArrayAddplaylist.append(false)
let x : Int = trackdata.classNumber as! Int
let className = String(x)
table_cell.img_play.image=#imageLiteral(resourceName: "play_track")
table_cell.lbl_Title.text = trackdata.songTitle
table_cell.lbl_SubTitle.text = String(format: "Class - %@ | %@", className,(trackdata.timeoftrack!))
table_cell.lbl_SubTitle.textColor = UIColor.lightGray
table_cell.trackidtext.text = "\(((constantsList.playTypeArray.arr_subCatagriesLecture.object(at: indexPath.row)as AnyObject).value(forKey: "track_id")as! Int))"
table_cell.cellselectionButton.tag = indexPath.row
constantsList.playTypeArray.imageLecture = ((constantsList.playTypeArray.arr_subCatagriesLecture.object(at: indexPath.row) as AnyObject).value(forKey: "image_url") as! String)
table_cell.img_Show.sd_setImage(with: URL(string: ((constantsList.playTypeArray.arr_subCatagriesLecture.object(at: indexPath.row) as AnyObject).value(forKey: "image_url") as! String)), placeholderImage: UIImage(named: "placeholder.png"))
// if(constantsList.playTypeArray.boolArrayAddplaylist[indexPath.row]) {
// // table_cell.tintColor = #colorLiteral(red: 0.9529411765, green: 0.7294117647, blue: 0.4549019608, alpha: 1)
// // table_cell.accessoryType = UITableViewCell.AccessoryType.checkmark
// table_cell.image_tick.image = UIImage(named:"round_done-1")
// } else {
// // table_cell.accessoryType = UITableViewCell.AccessoryType.none
// table_cell.image_tick.image = UIImage(named:"round")
// }
if constantsList.playTypeArray.dataArrayInPlaylist.count != 0 {
for travkID in constantsList.playTypeArray.dataArrayInPlaylist {
print(travkID)
if travkID as! Int == (constantsList.playTypeArray.arr_subCatagriesLecture.object(at: indexPath.row)as AnyObject).value(forKey: "track_id")as! Int {
table_cell.image_tick.image = UIImage(named:"round_done-1")
}else {
table_cell.image_tick.image = UIImage(named:"round")
}
}
}else {
table_cell.image_tick.image = UIImage(named:"round")
}
table_cell.selectionStyle = .none
return table_cell
}
func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
return "playlistaddition"
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
view.endEditing(true)
if cell.isSkeletonActive {
cell.hideSkeleton()
}
cell.transform = CGAffineTransform(translationX: 0, y: CELL_HEIGHT)
cell.layer.shadowColor = UIColor.black.cgColor
cell.layer.shadowOffset = CGSize(width: 10, height: 10)
cell.alpha = 0
UIView.beginAnimations("rotation", context: nil)
UIView.setAnimationDuration(0.5)
cell.transform = CGAffineTransform(translationX: 0, y: 0)
cell.alpha = 1
cell.layer.shadowOffset = CGSize(width: 0, height: 0)
UIView.commitAnimations()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// if searchBool == true {
// let cell = tableView.cellForRow(at: indexPath) as! playlistcelllaist
// let labelContent = cell.trackidtext.text
// print(labelContent!)
// constantsList.playTypeArray.track_idString = labelContent
// }else {
// print("Normal")
// }
}
При поиске и выборе UITableViewCell галочка должна также отображаться во всем списке табличных видов без выбора.Попробуйте дать мне решение с помощью моего кода или просто приведите свою логику на собственном примере