UITableViewCell очень медленно реагирует на выбор
У меня есть простой UITableViewController с базовой ячейкой.
didSelectRowAtIndexPath выполняет простую работу - просто сделайте UIAlertView и покажите его.
Проблема в том, что когда я нажимаю на строку, иногда я вижу предупреждение немедленно, иногда через несколько секунд (до 10 секунд).
Код
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
// Configure the cell...
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
NSLog("row clicked at index \(indexPath.row)")
let alert = UIAlertView(title: "Test", message: "Test message", delegate: self, cancelButtonTitle: "Done")
alert.show()
NSLog("alert showed")
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
В журнале я вижу
2015-08-06 20:51:54.591 experimental[10323:8602172] row clicked at index 2
2015-08-06 20:51:54.595 experimental[10323:8602172] alert showed
2015-08-06 20:52:00.901 experimental[10323:8602172] row clicked at index 3
2015-08-06 20:52:00.905 experimental[10323:8602172] alert showed
но на самом деле предупреждение не отображается на экране.
Любые предложения или указания, где найти решение, будут оценены.
Ответы
Ответ 1
Решение очень странно
вместо
cell.selectionStyle = UITableViewCellSelectionStyle.None
с
cell.selectionStyle = UITableViewCellSelectionStyle.Default
полностью решить проблему. После этого каждый щелчок по строке сразу отобразит результат.
Ответ 2
поместите его в функцию DispatchQueue.main.async.
DispatchQueue.main.async{
let alert = UIAlertView(title: "Test", message: "Test message",
delegate: self, cancelButtonTitle: "Done")
alert.show()
NSLog("alert showed")
}
и replace cell.selectionStyle = UITableViewCellSelectionStyle.None
до
cell.selectionStyle = UITableViewCellSelectionStyle.Default
Ответ 3
Если вы хотите, чтобы selectionStyle
был None, вы должны добавить метод alertView
в dispatch_async(dispatch_get_main_queue(),^{...});
или установите для параметра selectionStyle
значение по умолчанию.
Ответ 4
У меня была такая же проблема, определенно ошибка. В моем случае он добавлял дополнительные 150 мс перед загрузкой представления.
У меня была пользовательская ячейка таблицы с
cell.selectionStyle = UITableViewCellSelectionStyle.None
изменив его на
cell.selectionStyle = UITableViewCellSelectionStyle.Default
исправлена проблема...
Ответ 5
я обнаружил, что использование жестов лучше, чем делегат didselect, если в ячейке есть изображение
так
iv.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
tap.numberOfTapsRequired = 1;
[iv addGestureRecognizer:tap];
Ответ 6
В моем случае мне нужно иметь стиль выделения .none
потому что я использую настроенные ячейки. Поэтому я должен работать с didHighlightRowAt
и didUnhighlightRowAt
. Когда вы используете .default
вы не можете обрабатывать эти события.
Для меня DispatchQueue.main.async{}
сделал свое дело!
Ответ 7
Попробуйте установить:
delaysContentTouches = false