2015-03-24 9 views
6

Ho il seguente codice rapido per implementare UITableViewRowAction, quando faccio scorrere una riga la riga scorre verso sinistra come previsto, tuttavia tutte le intestazione di sezione sono anche scorrere verso sinistra con la riga della tabella contemporaneamente.Perché tutte le intestazioni di sezione si spostano con la cella di tabella quando implemento UITableViewRowAction

Ho incluso anche uno screenshot per mostrare ciò che sta accadendo

Se io rimuovere la sostituzione viewForHeader e sostituirlo con titleForHeaderInSection allora non ho alcun problema.

Il motivo per cui l'overhead viewForHeader è che voglio inserire un'immagine nella riga di intestazione.

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let cell = tableView.dequeueReusableCellWithIdentifier("header") as UITableViewCell 

    cell.textLabel?.text = instrumentGroups[section].name 
    cell.imageView?.image = UIImage(named: instrumentGroups[section].name) 

    return cell 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> InstrumentTableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("instrumentCell", forIndexPath: indexPath) as InstrumentTableViewCell 

    cell.instrument = instrumentGroups[indexPath.section].instruments[indexPath.row] 

    return cell 
} 

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { 

    let instrument = instrumentGroups[indexPath.section].instruments[indexPath.row] 

    var actions: [AnyObject] = [] 

    var action = UITableViewRowAction(style: .Normal, title: "Remove Watch") { (action, indexPath) -> Void in 
     tableView.editing = false 

     instrument.SetWatchList(false) 

     self.refresh() 
    } 

    action.backgroundColor = UIColor.redColor() 
    actions.append(action) 

    return actions 
} 

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

} 

As I slide the row to the left all the Section Rows slide as well

risposta

6

Ho avuto lo stesso problema. La risposta è semplice Perché si utilizza UITableViewCell come vista dell'intestazione. Prova invece UILabel o UITableViewHeaderFooterView.

23

Basta restituire cell.contentView anziché cell all'interno della funzione viewForHeaderInSection e il problema verrà risolto.

+2

E se la tua sezione ha un colore di sfondo, applicalo a 'contentView' invece che alla cella stessa. –

+0

Quando provo a farlo, la mia app si blocca. La CPU dice che usa il 99% ... –

Problemi correlati