UITableView, how do I know what Section during cellForRowAtIndexPath?

前端 未结 3 1302
隐瞒了意图╮
隐瞒了意图╮ 2021-02-03 16:39

I have a UITableView displaying a list of Cities. I want to separate them by State. I can\'t seem to figure out how to get it to pick the right items out of my Array. If Sectio

相关标签:
3条回答
  • 2021-02-03 17:38

    Swift 3, 4 and 4.2

    Using Switch

    switch indexPath.section {
            case 0:
                print("Write your code for section number one.")
            case 1:
                print("Write you code for section number two")
            default:
                return
            }
    

    Using if else

    if indexPath.section == 0 {
                print("Write your code for section number one.")
            } else if indexPath.section == 1 {
                print("Write your code for section number two.")
            }
    
    0 讨论(0)
  • 2021-02-03 17:39

    The method is called

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    The indexpath parameter contains a row and section property.

    indexPath.section  
    indexPath.row
    

    Here's documentation link for the NSIndexPath class.

    0 讨论(0)
  • 2021-02-03 17:45

    you can use indexPath.section and indexPath.row

    0 讨论(0)
提交回复
热议问题