2016-04-27 2 views
2

Я новичок в разработке мобильных приложений iOS и Swift. Я пытаюсь внедрить UITableViewController, который может выполнять следующие действия:Как переключить ячейки ячейки в UITableViewController в SWIFT?

  • При нажатии на любой из заголовков разделов ячейки, соответствующие этому разделу, должны опускаться.
  • После нажатия на заголовки разделов, в которых есть 0 ячеек, должен быть запущен сеанс segue и нажат на другой ViewController.

0 cells -> push to different ViewController.
n cells -> выпадающий список

Как достичь этого в Swift?

Я пробовал использовать следующий код в UITableViewController для переключения, но, напрасно.

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let frame: CGRect = tableView.frame 

    let button = UIButton(type: UIButtonType.System) as UIButton 
    button.frame = CGRectMake(5, 5, frame.size.width-10, 80) 
    button.addTarget(self, action: "menuButtonAction", forControlEvents: .TouchUpInside) 
    if section == 1 { 
     button.setTitle("Home", forState: UIControlState.Normal) 
    }else if section == 2 { 
     button.setTitle("Profile", forState: UIControlState.Normal) 
    }else if section == 3 { 
     button.setTitle("Projects", forState: UIControlState.Normal) 
    }else { 
     button.setTitle("Title", forState: UIControlState.Normal) 
    } 

    let headerView = UIView(frame: CGRectMake(0, 0, frame.size.width, 150)) 
    headerView.addSubview(button) 

    return headerView; 
} 

var enabledCategory:Bool = false 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    if (section == 3 && enabledCategory){ 
     return dataHandler.getNumberOfProjects() 
    } 
    else { 
     return 0 
    } 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("menuCellIdentifier", forIndexPath: indexPath) as? TableViewCell 

    // Configure the cell... 

    if indexPath.section == 3 { 
     cell!.customLabel.text = String(indexPath.row) 
    } 
    return cell! 
} 


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if indexPath.section == 3 { 
     enabledCategory = !enabledCategory 
     tableView.reloadSections(NSIndexSet(index: 3) , withRowAnimation: .Fade) 
    } 
} 

И, я не имею ни малейшего представления о том, как нажать на другую ViewController, нажав на разделы 1 и 2.

Спасибо.

+0

Нет. Вы должны предоставить то, что попробовали, тогда мы сможем помочь вам в этом. Никто не напишет ваш код для вас. – rMickeyD

ответ

0

По умолчанию заголовки UITableView не могут быть изменены. Вы должны реализовать метод tableView(_:viewForHeaderInSection:)UITableViewDelegate и предоставить свой собственный UIViewUIButton или какой-либо элемент, который можно щелкнуть.

+0

Я добавил метод UIButton в viewForHeaderInSection. Как перейти к переключению ячеек и нажатию на другой ViewController? – Shashank

Смежные вопросы