0

セルを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);


  */  //}
}
4

2 に答える 2

2

これに関するあなたの考えは正しくありません。セルのタイトルを設定するという観点から考えるのではなく、テーブル ビューにデータを入力するために使用する配列を更新することを考えてください。これは、アプリの簡略化されたバージョンです。追加ボタンをクリックすると、行 0 に新しいセルが追加され、ラベルのテキストは「新しい習慣」になります。そのセル (またはその他のセル) をクリックすると、文字列を選択するピッカー ビューを備えたコントローラーに移動し、その文字列がデリゲート メソッドでテーブル ビューに返されます。そのメソッドでは、配列 myCells を、テーブルの indexPathForSelectedRow から取得した正しいインデックスで渡された文字列で更新し、テーブル ビューで 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) は次のとおりです。

@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]];
}
于 2013-08-17T05:42:10.170 に答える
1

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;
于 2013-08-17T04:09:03.447 に答える