简体   繁体   中英

UITableView - insert new row by tapping on existing cell

I use UITableView with prototype cells and would like the top most cell to be an "adder" cell: when user taps on that cell, the segue is being performed. As the UITableView is being dynamically generated (except the top one) and cells are being reused, I have two question:

1 - is it possible to make one static cell while all the others will remain prototype cells?

2 - If not, how to implement this solution? I think, it has to be implemented in

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

but I have no idea, is this method right place to do it. Am I correct, to implement this custom cell, I need to check, whether indexPath.row = 0 and substitute the needed cell with custom, while programmatically shifting all the cells one row down?

Yiu can do on the Tap of Your cell, you can create two different cell, one your prototype cell and one for your details

in your

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        AdderCell *cell = (AdderCell*)[tableView dequeueReusableCellWithIdentifier:@"AdderCell" forIndexPath:indexPath];
     } else {
       CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
     }
}

on tap of your Adder cell in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
                // add data to your row array and reload the tableRow
             } else {
               // do your tap action
             }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM