UITableView: Handle cell selection in a mixed cell table view static and dynamic cells

后端 未结 6 1984
谎友^
谎友^ 2020-12-05 17:30

I am trying to mix dynamic and static cells in a grouped table view: I would like to get two sections with static cells at the top followed by a sec

6条回答
  •  醉酒成梦
    2020-12-05 17:37

    Thank you applefreak for comprehenisve answer. I'll add my suggestion here that you don't need any section calculations if you add a dummy sections for the static ones. You can even add placeholder elements there. This way you don't have to recreate NSIndexPaths. One important thing is to override each and every method that uses NSIndexPaths as these would crash the app due to array index mismatch.

    Also, I gave up using the dequeue mechanism completely and created the cell statically as there weren't that many cells for the dynamic content I had. This makes tableView:cellForRowAtIndexPath: very simple.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0)
        {
            return _locationCells[indexPath.row];
        }
        else
        {
            return [super tableView:tableView cellForRowAtIndexPath:indexPath];
        }
    }
    

    Just remember the placeholder sections.

提交回复
热议问题