detailTextLabel не отображается, но установлено на 100%

У меня есть MGSwipeTableCell, добавленный в мой tableView следующим образом:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let reuseIdentifier = "Cell"
    var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
    if cell == nil {

        cell = MGSwipeTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier)
    }
    cell.delegate = self
    cell.backgroundColor = blue
}

Все работает нормально. Но теперь, если я нажму на ячейку, цвет ячейки изменится, может быть, на секунду на белый, а затем на зеленый (цвет по умолчанию — синий). Вот что должно произойти, если ячейка была нажата:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  let mySelectedCell = tableView.cellForRowAtIndexPath(indexPath) as! MGSwipeTableCell!
  mySelectedCell.backgroundColor = green
  mySelectedCell.detailTextLabel?.text = "Test" 
}

Таким образом, цвет должен измениться только на зеленый, а не на белый, а затем на зеленый, и должен отображаться detailTextLabel с текстом «Тест». Я надеюсь, что вы можете помочь мне решить эту проблему. Я действительно не знаю, что делать.

Решение:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let reuseIdentifier = "Cell"
    var cell =  tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
    cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle,  reuseIdentifier: reuseIdentifier)
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.delegate = self
    cell.backgroundColor = blue

}

person 123    schedule 16.11.2015    source источник
comment
Где и как вы создаете mySelectedCell объект, который используете в tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)?   -  person Alexey Pichukov    schedule 16.11.2015
comment
UITableViewCellStyle.Default не имеет подзаголовка. Вы должны использовать .Value1, .Value2 или .Subtitle в зависимости от того, как вы хотите, чтобы ваша ячейка выглядела. developer.apple.com /library/ios/documentation/UIKit/Reference/   -  person Tyrelidrel    schedule 16.11.2015
comment
@alex_p Это была моя вина, что я должен быть ячейкой вместо mySelectedCell. Я обновил вопрос.   -  person 123    schedule 16.11.2015
comment
@Tyrelidrel Не помогло. Перепробовал все 3 стиля, все равно не работает.   -  person 123    schedule 16.11.2015
comment
Какое значение поля Selection для вашей пользовательской ячейки в Attributes inspector в раскадровке?   -  person Alexey Pichukov    schedule 16.11.2015
comment
Выбор установлен по умолчанию.   -  person 123    schedule 16.11.2015
comment
Попробуйте установить None. Похоже, ячейка выбрана, а затем отбрасываете ее.   -  person Alexey Pichukov    schedule 16.11.2015
comment
У тебя есть другие идеи?   -  person 123    schedule 16.11.2015


Ответы (1)


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let reuseIdentifier = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
if cell == nil {
    //Change this line
    cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
}
//And this line
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.delegate = self
cell.backgroundColor = blue
}

оставить все как есть внутри "didSelectRowAtIndexPath"

person Shabarinath Pabba    schedule 16.11.2015
comment
Большое спасибо за твою помощь! Теперь смена цветов работает, но detailTextLabel по-прежнему не отображается. - person 123; 17.11.2015
comment
в didSelectRowAtIndexPath, можете ли вы поставить точку останова после mySelectedCell.backgroundColor = green, а затем в консоли po mySelectedCell.detailedTextLabel? - person Shabarinath Pabba; 17.11.2015
comment
Я решил это в функции cellForRowAtIndexPath (см. мой вопрос). Спасибо за вашу помощь! - person 123; 17.11.2015
comment
отлично, без проблем :D - person Shabarinath Pabba; 18.11.2015