基本上,我有一个由各部分分隔的tableView。
tableView允许选择多个行,并在所有选中的行上显示一个附件.checkmark。
如果用户开始选择一个部分下的行,然后尝试选择其他部分下的其他行,则我希望出现警报消息并且无法进行选择。
以下是到目前为止的代码:
import UIKit
class TableViewController: UITableViewController {
var Name = UserDefaults.standard.string(forKey: "name")!
var sections = [Section]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
navigationController?.navigationBar.prefersLargeTitles = true
fetchJSON()
self.tableView.allowsMultipleSelection = true
}
}
答案 0 :(得分:1)
实施UITableViewDelegate
方法tableView(_:willSelectRowAt:)
。
使用 map(_:)
从sections
获取indexPathsForSelectedRows
。
使用 indexPath's
检查section
中的tableView(_:willSelectRowAt:)
sections
是否包含在先前获得的array
contains(_:)
中>
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let sections = tableView.indexPathsForSelectedRows?.map({ $0.section }) {
if !sections.contains(indexPath.section) {
//Show Alert here....
let alert = UIAlertController(title: "Alert..!!", message: "You're selection row from Section:\(indexPath.section)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
return nil
}
}
return indexPath
}
tableView(_:willSelectRowAt:)返回值:索引路径对象 确认或更改所选行。返回一个NSIndexPath对象 如果要选择另一个单元格,则除了indexPath以外。返回 如果您不希望选择该行,则为nil。
答案 1 :(得分:0)
据我了解,您可以执行以下操作:
struct Section {
let name : String
var hasItemsSelected: Bool
let items : [Portfolios]
}
didSelectRowAt
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//when you select a row check if there is another row selected on another section by checking if the hasItemsSelected is true on all other sections
//1. create an array containing all section besides the current section
let allOtherSections = sections
.enumerated()
.filter { $0.offset != indexPath.section }
.map { $0.element }
//2. if allOtherSections does not have seledcted items
if allOtherSections.allSatisfy { $0.hasItemsSelected == false } {
self.sections[indexPath.section].hasItemsSelected = true
tableView.cellForRow(at: ind exPath)?.accessoryType = .checkmark
} else {
let numberOfSelectedRowsOnCurrentIndexPath = tableView.indexPathsForSelectedRows?.enumerated().filter { $0.offset != 1 }.count
if numberOfSelectedRowsOnCurrentIndexPath == 0 {
self.sections[indexPath.section].hasItemsSelected = false
}
tableView.deselectRow(at: indexPath, animated: true)
//show the alert here
let alertController = UIAlertController(title: "Title", message: "Your message", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
希望我能帮助您
答案 2 :(得分:0)
签出tableView(_:willSelectRowAt:)。您可以从此委托方法返回nil以防止选择行。
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let
indexPathsForSelectedRows = tableView.indexPathsForSelectedRows, indexPathsForSelectedRows.count > 0,
indexPath.section != indexPathsForSelectedRows[0].section
{
// If there is at least one row already selected
// And the section of that row is different than the section of the row about to be selected
// Don't allow the selection
let alertController = UIAlertController(title: "Whoops!", message: "Don't do that!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
return nil
}
// There's either nothing selected yet or the section of this row is the same as those already selected
return indexPath
}
答案 3 :(得分:0)
我认为,每个示例都需要为其创建一个额外的逻辑。 您可以将所有选定的indexPath放在一个数组中,并始终将第一个项目(indexPath)与要添加的每个项目进行比较,indexPath包含属性Section,因此只需要比较该部分是否相等,是否并非如此,您会显示所需的警报,然后手动取消选择该项目。
val channelId =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{ createNotificationChannel("111", "Speed Monitor Service") } else { "" }
val remoteView = RemoteViews(packageName, R.layout.notification)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_background) //this method only accepts int not Icon
.setCustomContentView(remoteView)
.build()
startForeground(1, notification)