2015-08-06 13 views
11

Ho un semplice UITableViewController con cella base. didSelectRowAtIndexPath fai un lavoro semplice: basta creare UIAlertView e mostrarlo.UITableViewCell risposta molto lenta su select

Il problema è quando tocco una riga a volte vedo l'avviso immediatamente, a volte dopo pochi secondi (fino a 10 secondi).

Il codice è

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 
} 

Nel registro vedo

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 

ma in realtà avviso non mostra sullo schermo.

Qualsiasi suggerimento o indicazione in cui trovare una soluzione sarebbe apprezzato.

+0

Il tuo codice funziona correttamente. –

+0

Si dovrebbe verificare con gli strumenti. Questo codice sembra buono. – derdida

+2

In realtà UIAlertView è deprecato. Usa UIAlertController. – stosha

risposta

16

La soluzione è molto strano

sostituzione

cell.selectionStyle = UITableViewCellSelectionStyle.None 

con

cell.selectionStyle = UITableViewCellSelectionStyle.Default 

risolve completamente il problema. Dopo che lo ogni clic sulla riga mostrerà immediatamente il risultato.

+0

Davvero strano. Io uso UITableViewCellSelectionStyleBlue e ho lo stesso problema. Passando a UITableViewCellSelectionStyle.Default funziona per me. – derjohng

+0

Esattamente nel mio caso !!!!! grazie molto apprezzato. – Preetha

+0

È strano! Funziona anche per me, grazie amico. Qualche idea? cosa dobbiamo fare se dobbiamo sbarazzarci dello stile di selezione. –

0

Ho avuto esattamente lo stesso problema, sicuramente un bug. Nel mio caso aggiungeva 150ms in più prima di caricare la vista.

ho avuto una cella di tabella personalizzata con

cell.selectionStyle = UITableViewCellSelectionStyle.None 

cambiarlo con

cell.selectionStyle = UITableViewCellSelectionStyle.Default 

risolto il problema ...

0

ho trovato che l'uso gesto è meglio di didselect delegato se ci è l'immagine sulla cella

so

iv.userInteractionEnabled = YES; 

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; 

tap.numberOfTapsRequired = 1; 
[iv addGestureRecognizer:tap]; 
3

metterlo nella funzione DispatchQueue.main.async.

DispatchQueue.main.async{

let alert = UIAlertView(title: "Test", message: "Test message",delegate: self, cancelButtonTitle: "Done") alert.show() NSLog("alert showed") }

e replace cell.selectionStyle = UITableViewCellSelectionStyle.None a cell.selectionStyle = UITableViewCellSelectionStyle.Default

+0

Il messaggio asincrono ha fatto il trucco. Usando selectionStyle come None. –

1

Se si desidera selectionStyle ad essere nessuno, si dovrebbe aggiungere il metodo alertView a dispatch_async(dispatch_get_main_queue(),^{...}); oppure impostare selectionStyle su Predefinito.