Top cell name changes when changing any cell name

前端 未结 2 935
[愿得一人]
[愿得一人] 2021-01-26 17:59

I have a HabitViewController (UITableViewController) with a button to add cells. When a cell is added its default title is \"New Habit\". Then the user

2条回答
  •  粉色の甜心
    2021-01-26 18:07

    Your thinking on this is not right. You shouldn't think in terms of setting the title of a cell, but instead think about updating the array that you use to populate the table view with data. Here is a simplified version of your app. When you click the add button it adds a new cell in row 0 with the label's text being "New Habit". When you click on that cell (or any other), it takes you to the controller with the picker view where you choose a string, and that string is passed back to the table view in a delegate method. In that method I update the array, myCells with that passed in string at the correct index gotten from the table's indexPathForSelectedRow, and then call reloadData on the table view to update it's view.

    This is the table view controller:

    #import "TableController.h"
    #import "ViewController.h"
    
    @interface TableController ()
    @property (strong,nonatomic) NSMutableArray *myCells;
    @end
    
    @implementation TableController 
    
    - (IBAction)insertNewObject:(UIBarButtonItem *)sender
    {
        if (!self.myCells) {
            self.myCells = [[NSMutableArray alloc] init];
        }
        [self.myCells insertObject:@"New Habit" atIndex:0];
        NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.myCells.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.textLabel.text = self.myCells[indexPath.row];
        return cell;
    }
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        ViewController *vc = segue.destinationViewController;
        vc.delegate = self;
    }
    
    -(void)setCellName2:(NSString *)cellName {
        NSInteger selectedRow = [self.tableView indexPathForSelectedRow].row;
        [self.myCells replaceObjectAtIndex:selectedRow withObject:cellName];
        [self.tableView reloadData];
    }
    

    Here is the controller (ViewController) with the picker view:

    @interface ViewController ()
    @property (strong,nonatomic) NSArray *pickerData;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.pickerData = @[@"Posture",@"Paludies Abbs",@"Custom"];
    }
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        return 1;
    }
    
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        return [self.pickerData count];
    }
    
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        return [self.pickerData objectAtIndex:row];
    }
    
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        [self.delegate setCellName2:self.pickerData[row]];
    }
    

提交回复
热议问题