更改任何单元名称时,顶部单元名称会更改

我有一个HabitViewController (UITableViewController)与一个button来添加单元格。 当一个单元格被添加时,它的默认标题是“New Habit”。 然后用户可以点击该单元格,然后出现一个detailViewController和一个select器来select习惯。 然后将cell.label.text设置为选取器中的选定选项。 这是我的问题所在。 例如,如果我通过按下button三次添加3个单元格,然后select第三行。 然后我select了“Hello World”选项。 顶部的单元格将被命名为hello world,而不是第三个单元格。 这是正常的吗? 这是我的代码:

HabitViewController.h

 #import <UIKit/UIKit.h> #import "DetailViewController.h" @interface HabitViewController : UITableViewController <DetailViewDelegate> { } @property (nonatomic, strong) NSString *cellNameSender; @property (strong, nonatomic) IBOutlet UITableView *tableView; @property (strong, nonatomic) NSIndexPath *selectedCell; @end 

.M

 #import "HabitViewController.h" #import "DetailViewController.h" @interface HabitViewController () { NSMutableArray *myCells; } @property(strong, nonatomic) NSString *cellName2; @end @implementation HabitViewController @synthesize cellNameSender, selectedCell; - (void)awakeFromNib { [super awakeFromNib]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.navigationItem.leftBarButtonItem = self.editButtonItem; [self.editButtonItem setTintColor:[UIColor colorWithRed:.33 green:.33 blue:.33 alpha:1]]; UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; self.navigationItem.rightBarButtonItem = addButton; addButton.tintColor = [UIColor colorWithRed:.33 green:.33 blue:.33 alpha:1]; [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar.png"] forBarMetrics:UIBarMetricsDefault]; } - (void)viewDidAppear:(BOOL)animated { [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)insertNewObject:(id)sender { if (!myCells) { myCells = [[NSMutableArray alloc] init]; } [myCells insertObject:@"New Habit" atIndex:0]; NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic]; } #pragma mark - Table View - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return myCells.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; cell.textLabel.text = myCells[indexPath.row]; return cell; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [myCells removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { } } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { } - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { DetailViewController *vc = segue.destinationViewController; vc.delegate = self; } #pragma mark - DetailViewDelegate -(void)setCellName2:(NSString *)cellName { NSInteger selectedRow = [self.tableView indexPathForSelectedRow].row; [myCells replaceObjectAtIndex:selectedRow withObject:cellName]; [self.tableView reloadData]; } @end 

DetailViewController.h

 #import <UIKit/UIKit.h> @protocol DetailViewDelegate <NSObject> - (void)setCellName2:(NSString *)cellName; @end @interface DetailViewController : UIViewController<UIPickerViewDelegate> { NSArray *PickerData; } @property (weak, nonatomic) IBOutlet UITextField *habitField; @property (strong, nonatomic) UIToolbar *toolBar; @property (weak, nonatomic) id<DetailViewDelegate> delegate; @property (nonatomic, strong) NSString *cellName; @property (nonatomic, strong) UIBarButtonItem *backButton; @property (nonatomic, strong) UIPickerView *Picker; @property (retain, nonatomic) NSArray *PickerData; @property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton; @property (nonatomic, strong) UIBarButtonItem *barDoneButton; @property (nonatomic, strong) UIBarButtonItem *flexSpace; @property (nonatomic, strong) NSString *customHabit; - (IBAction)backToRoot:(id)sender; @end 

.M

 #import "DetailViewController.h" #import "HabitViewController.h" @interface DetailViewController () { } @end @implementation DetailViewController @synthesize PickerData, Picker, toolBar, backButton, barDoneButton, flexSpace; - (void)viewDidLoad { [super viewDidLoad]; self.pickerData = @[@"Posture",@"Paludies Abbs",@"Custom"]; [self.delegate setCellName2:self.cellName]; toolBar = [[UIToolbar alloc] init]; toolBar.barStyle = UIBarStyleBlackOpaque; [toolBar sizeToFit]; [toolBar setBackgroundImage:[UIImage imageNamed:@"red_navigation_bar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; // Done button on toolbar barDoneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(releasePicker)]; // Back button on toolbar backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"style:UIBarButtonItemStyleDone target:self action:@selector(backToPicker)]; // Habit PickerView Picker = [[UIPickerView alloc] init]; Picker.showsSelectionIndicator = YES; Picker.delegate = self; barDoneButton.image = [UIImage imageNamed:@"button.png"]; // Toolbar above picker [toolBar setItems:@[flexSpace, barDoneButton] animated:YES]; self.habitField.inputAccessoryView = toolBar; [self.habitField addTarget:self action:@selector(customHabitChanged) forControlEvents:UIControlEventEditingChanged]; [self.habitField setInputView:Picker]; } - (void)customHabitChanged { self.customHabit = self.habitField.text; self.cellName = self.customHabit; NSLog(@"%@", self.customHabit); [self.delegate setCellName2:self.cellName]; } - (void)backToPicker { [toolBar setItems:@[flexSpace, barDoneButton] animated:YES]; [self.habitField resignFirstResponder]; [self.habitField setInputView:Picker]; [self.habitField becomeFirstResponder]; } - (void)releasePicker { [self.habitField resignFirstResponder]; [self.habitField setInputView:Picker]; [toolBar setItems:@[flexSpace, barDoneButton] animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)backToRoot:(id)sender { [self.navigationController popToRootViewControllerAnimated:YES]; } -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [PickerData count]; } -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [PickerData objectAtIndex:row]; } -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { [self.delegate setCellName2:self.PickerData[row]]; /* int select = row; if (select == 0) { self.cellName = @"Posture"; self.habitField.text = @"Posture"; [self.delegate setCellName2:self.cellName]; NSLog(@"%@ Is Selected", self.cellName); } if (select == 1) { self.cellName = @"Palaudies Abbs"; self.habitField.text = @"Palaudies Abbs"; [self.delegate setCellName2:self.cellName]; NSLog(@"%@ Is Selected", self.cellName); } if (select == 2) { [self.habitField resignFirstResponder]; [self.habitField setInputView:nil]; [self.habitField becomeFirstResponder]; [toolBar setItems:@[backButton, flexSpace, barDoneButton] animated:YES]; self.habitField.text = @""; self.habitField.placeholder = @"Custom"; [self.delegate setCellName2:self.cellName]; NSLog(@"%@ Is Selected", self.cellName); */ //} } 

IN cellForRowAtIndexPath

 if(indexPath.row == object.count-1) cell.textLabel.text = @"New Habit"; else { NSDate *object = _objects[indexPath.row]; cell.textLabel.text = [object description]; } return cell; 

你对此的想法是不正确的。 您不应该考虑设置单元格的标题,而应考虑更新用于使用数据填充表格视图的数组。 这里是你的应用程序的简化版本。 当你点击添加button时,它会在第0行添加一个新的单元格,标签的文本是“New Habit”。 当你点击那个单元格(或者其他任何单元格)时,它将把你带到select器视图的控制器中,在那里你select一个string,并且该string通过一个委托方法传递回表格视图。 在那个方法中,我更新数组,myCells与从表的indexPathForSelectedRow获取的正确索引中的string传递,然后调用表视图上的reloadData来更新它的视图。

这是表视图控制器:

 #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]; } 

这里是控制器(ViewController)与select器视图:

 @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]]; }