1

これまでのところ、新しいセルを追加するためのボタン付きの を作成しUITableViewました。HabitViewControlleセルをタップすると adetialViewControllerが表示されます。このビューでは、それらは でありtextField、タップするUIPickerと が起動されます。tableViewCellのタイトルをピッカー選択に設定したい..string呼び出しcellNameを行い、それを他の に送信できますViewController。また、pickerが変更されたcellNameときに、ピッカー選択が選択されたときにセルにしたいタイトルに文字列を設定するようにしました。したがって、cellNameが設定されているときに実際にセルのタイトルを文字列に設定し、ピッカーの選択が変更されたことを除いて、ほとんど準備ができています。これが意味をなさない場合は、私にコメントを送ってください。質問を言葉で書くのが難しいので、私は助けようとします

習慣ViewController.h

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface HabitViewController : UITableViewController <DetailViewDelegate> {

    UITableViewCell *cell;
}

@end

.m

#import "HabitViewController.h"

#import "DetailViewController.h"

@interface HabitViewController () {
    NSMutableArray *_objects;

}
@property(strong, nonatomic) NSString *cellName2;


@end

@implementation HabitViewController

- (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];

    //if ([cellName isEqualToString:@"Hello World"]) {
   // }

}
- (void)viewDidAppear:(BOOL)animated {
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];

    cell.textLabel.text = @"New Habit";

    NSLog(@"%@",self.cellName2);

    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }

 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"showDetail"]) {

        /* assigning self as delegate, telling the detail view that I implement
         * setCellName2:, so it (the detailVC) can call it whenever it wants to.
         */
        [segue.destinationViewController setDelegate:self];
    }
}
#pragma mark - DetailViewDelegate

// note: this is just a property setter so this is not actually needed
- (void)setCellName2:(NSString *)cellName {
    cellName = cellName;

   NSLog(@"%@",cellName);

    cell.textLabel.text = cellName;
}




@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 (weak, nonatomic) id<DetailViewDelegate> delegate;

@property (nonatomic, strong) NSString *cellName;

@property (retain, nonatomic) NSArray *PickerData;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;

- (IBAction)backToRoot:(id)sender;

@end

.m

#import "DetailViewController.h"

#import "HabitViewController.h"

@interface DetailViewController () {
    UITableViewCell *_cell;

}

@end

@implementation DetailViewController

@synthesize PickerData;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray *array = [[NSArray alloc] initWithObjects:@"Posture",@"Paludies Abbs",@"Custom", nil];
    self.PickerData = array;

    [self.delegate setCellName2:self.cellName];

    UIToolbar *toolBar = [[UIToolbar alloc] init];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    [toolBar sizeToFit];

    [toolBar setBackgroundImage:[UIImage imageNamed:@"red_navigation_bar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                               target:self
                                                                               action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:self
                                                                                action:@selector(releasePicker)];


    UIPickerView *Picker = [[UIPickerView alloc] init];
    Picker.showsSelectionIndicator = YES;
    Picker.delegate = self;
    doneButton.image = [UIImage imageNamed:@"button.png"];

    [toolBar setItems:@[flexSpace, doneButton] animated:YES];
    self.habitField.inputAccessoryView = toolBar;

    [self.habitField setInputView:Picker];

}
- (void)releasePicker {
    [self.habitField resignFirstResponder];
}

- (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 [self.PickerData objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    int select = row;
    if (select == 0) {
       self.cellName = @"Posture";

        [self.view reloadInputViews];

       NSLog(@"%@ Is Selected", self.cellName);
    }
}


@end
4

2 に答える 2

1

デリゲートを使用して、didSelectRow メソッドでピッカーを設定したいと思います。このような:

DetailViewController.m:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    int select = row;
    if (select == 0) {
       self.cellName = @"Posture";

       [self.delegate setCellName2:self.cellName];

       NSLog(@"%@ Is Selected", self.cellName);
    }
}

そして、habitViewController に戻ります:

- (void)setCellName2:(NSString *)cellName {
    cellName = cellName;

    NSLog(@"%@",cellName);

    selectedCell.textLabel.text = cellName;
    [self.tableView reloadData];
}

「セル」にもいくつか問題があります。多くの行があると仮定すると、これは何度も何度も再利用され、最後に表示されたセルが常に含まれますが、これは期待どおりになることはめったにありません。

名前を selectedCell に変更し、セルがタッチされて他のビューがトリガーされたときに設定します。

 @interface HabitViewController : UITableViewController <DetailViewDelegate> {

    UITableViewCell *savedCell;
}

ここに設定します:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"showDetail"]) {
        if ([sender isKindOfClass[UITableViewCell class]]) {
            savedCell = (UITableViewCell *)sender;
        }

        /* assigning self as delegate, telling the detail view that I implement
         * setCellName2:, so it (the detailVC) can call it whenever it wants to.
         */
        [segue.destinationViewController setDelegate:self];
    }
}

このメソッドでは一時セルを使用します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];

    cell.textLabel.text = @"New Habit";

    NSLog(@"%@",self.cellName2);

    return cell;
}
于 2013-08-16T00:34:42.173 に答える
0

これで直りました

最初に、habitViewController.h で次のことを行います。

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface HabitViewController : UITableViewController <DetailViewDelegate> {

    UITableViewCell *cell;
}

@property (nonatomic, strong) NSString *cellNameSender;

@end

次に、.m で、次のようなコードを次のように置き換えます。

- (void)setCellName2:(NSString *)cellName {
    cellName = cellName;

    NSLog(@"%@",cellName);

    self.cellNameSender = cellName;
}

@end

そしてこれも同じ

- (void)viewDidAppear:(BOOL)animated {

    cell.textLabel.text = self.cellNameSender;
}

これで、tableViewCell は姿勢が選択されていることを示すはずです。

于 2013-08-16T21:13:34.577 に答える