Adding dynamic sub-rows by selecting tableview row in tableview iPhone errors?

后端 未结 2 1293
一生所求
一生所求 2021-01-25 15:12

I want to add row dynamically. I have tableview list of building names. If some one choose building(didSelectRowAtIndexPath) then respective floors of building should get added

相关标签:
2条回答
  • 2021-01-25 15:56
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath 
    {
        //NSIndexPath *selectedIndexPath = [self.indoortable indexPathForSelectedRow];
    zonesFloorA = [[NSArray alloc] initWithObjects:@"Gr fl",@"1st fl",@"2nd fl",nil];
    
    if (tableView == indoortable )
    {
        for (NSString *str in zonesFloorA) {
            [indoorZones addObject:str];
       }
       //[[self indoortable] beginUpdates];
       //[[self indoortable] insertRowsAtIndexPaths:(NSArray *)zonesFloor  withRowAnimation:UITableViewRowAnimationNone];
       //[[self indoortable] endUpdates];
    }
    if (tableView == indoortable_iPad )
    {
    
    //some logic
     }
    
      [tableView reloadData];
    
    }
    

    may this meet your requirement

    0 讨论(0)
  • 2021-01-25 16:02

    Okay, so not to sound mean, but there are almost too many issues here to count.

    Let's start with a basic explanation of how tableView's work so that you can start to fix this:

    First, the tableView asks how many sections are in the table by calling:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    }
    

    In your code, you simply tell it that it has one section. However, in your later code, when you try to add rows to your table, you tell it that you want to add your rows to a second section (with an index of 1). Therefore, you either need to add these rows to section 0 instead, or update the above method to tell it that, sometimes, there are two sections.

    Second, the tableView asks how many rows are in each section of the table by calling:

    - (NSInteger)tableView:(UITableView *)tableView 
                 numberOfRowsInSection:(NSInteger)section {
    
    }
    

    In your code, you are simply returning the number of zones. However, like above, you need to include the rows that you have added to your table. If you are adding them to a different section, then you need to return different values, depending on how many rows are in the section with the index asked for in the section variable. If they are all in the same section, then you need to add them up and return the correct value.

    Third, the tableView asks for an actual cell for the row by calling:

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

    In your code, you are only returning a cell which has data populated by the indoorZones array, but you also need to supply cells which are configured properly for the specific zone/floor. Again, you either need to determine this by section number or row number as appropriate.

    Finally, when you click on a row, the tableview tells you by calling the following method:

    - (void)tableView:(UITableView *)tableView 
            didSelectRowAtIndexPath:(NSIndexPath   *)indexPath {
    
    }
    

    In this method, you need to update your data source that is used by the previous functions so that when they get called again, they will provide the correct data. Your data source must mirror the way that you want your table view to look. In your case, you have an array called indoorZones. This is good if you just want to display a list of zones, which is what you start off doing. However, when you want to add more rows, you need to add more rows to your data source first, so that when the tableView starts this process over, it is already there.

    If you want everything to stay in one section, then I would come up with a data source that can include both types of rows, and be able to distinguish between them so that cellForRowAtIndexPath can create the proper type of cell and return it.

    If you want two sections, then I would add a second array for the second section (since it is not the same type of data) and return the appropriate values in each of these methods, based on which array you need to use for that section.

    I hope this helps!

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