Я уже создал табличное представление, которое позволяет пользователю добавлять задачи в табличное представление, но теперь мне нужно добавить флажок перед каждой задачей, чтобы пользователь мог выбирать, какие задачи он/она хочет отправить другому контроллеру представления.
После того, как я погуглил, я понял, что в Swift нет флажка или радиокнопки, мне нужно создать свой собственный "boxChecked.png" и "boxUnchecked.png",
Вот фрагмент моего кода, который выдает ошибку:
if (selectedIndex == indexPath) {
cell.radioButton?.setImage(UIImage(named: "boxChecked"),forState:UIControlState.Normal)
} else {
cell.radioButton?.setImage(UIImage(named: "boxUnchecked"),forState:UIControlState.Normal)
}
Ошибка:
'UITableViewCell?' не имеет члена с именем "radiobutton"
хотя я пытался создать класс с именем TableViewCell, который является подклассом UITableViewCell, и объявил в нем переменную radioButton следующим образом:
@IBOutlet weak var radioButton: UIButton!
но это все равно не сработало.
Вот код func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
if (selectedIndex == indexPath) {
cell.radioButton?.setImage(UIImage(named: "boxChecked"),forState:UIControlState.Normal)
} else {
cell.radioButton?.setImage(UIImage(named: "boxUnchecked"),forState:UIControlState.Normal)
}
cell.textLabel?.textAlignment = .Right
cell.detailTextLabel?.textAlignment = .Left
cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].score
return cell
//let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default")
// cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
// cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].desc
//return cell
}
Подробнее здесь: https://stackoverflow.com/questions/333 ... -tableview