セルをHabitViewController
(UITableViewController)
追加するためのボタンがあります。セルが追加されると、デフォルトのタイトルは「新しい習慣」になります。次に、ユーザーがセルをタップすると、detailViewController がピッカーと共に表示され、習慣を選択できます。cell.label.text は、ピッカーで選択されたオプションに設定されます。これが私の問題です。たとえば、ボタンを 3 回押して 3 つのセルを追加した場合、3 番目の行を選択します。そして、Hello World オプションを選択しました。一番上のセルは、3 番目のセルではなく、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);
*/ //}
}