根据在文本字段中键入的内容设置tableViewCell的标题

时间:2013-08-18 01:28:17

标签: iphone objective-c uitableview

现在,我有一个带有按钮的tableViewController来添加更多内容。点击按钮时,会出现一个标题为“New Habit”的新单元格。轻触单元格时,DetailViewController将显示文本字段。点击文本字段时,会显示一个UIPicker,其中包含三个选项。首先是“姿势”,第二个是“Palaudies Abbs”,第三个选项是自定义,当选择自定义时,键盘被替换为键盘。使用键盘输入自定义习惯,它将显示在文本字段中。选择Posture或palaudies时,tableViewCell将根据选择命名(您可以在代码中看到)。现在问题是将自定义选项设置为单元格的标题。目前我已经能够接受字符串,我试图将其设置为删除,就像其他人一样,但它最终将标题设置为空。

这是我的代码

DetailViewController.h

#import <UIKit/UIKit.h>

@protocol DetailViewDelegate <NSObject>
- (void)setCellName2:(NSString *)cellName;
@end

@interface DetailViewController : UIViewController<UIPickerViewDelegate> {
}
@property (nonatomic, strong) id <DetailViewDelegate> delegate;
@property (nonatomic, strong) UIToolbar *toolBar;
@property (nonatomic, strong) UIBarButtonItem *backButton;
@property (nonatomic, strong) UIPickerView *Picker;
@property (nonatomic, strong) UIBarButtonItem *barDoneButton;
@property (nonatomic, strong) UIBarButtonItem *flexSpace;
@property (nonatomic, strong) NSString *customHabit;
@property (nonatomic, strong) NSString *cellNames;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *doneButton;
@property (nonatomic, strong) IBOutlet UITextField *habitField;
- (IBAction)backToRoot:(id)sender;
@end

的.m

#import "DetailViewController.h"

#import "HabitViewController.h"

@interface DetailViewController ()

@property (retain, nonatomic) NSArray *pickerData;

@end

@implementation DetailViewController

@synthesize  Picker, toolBar, backButton, barDoneButton, flexSpace, cellNames;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.pickerData = @[@"Posture",@"Paludies Abbs",@"Custom"];

    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:UIControlEventEditingDidEnd];

    [self.habitField setInputView:Picker];

    cellNames = @"New Habit";

}
- (void)customHabitChanged {
    self.customHabit = self.habitField.text;
    cellNames = self.customHabit;
}
- (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];
}

- (IBAction)backToRoot:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(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[row];
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    cellNames = [[NSString alloc] init];
    int select = row;
    if (select == 0) {
        cellNames = @"Posture";

        [self.delegate setCellName2:cellNames];

    }
    if (select == 1) {

        self.habitField.text = @"Palaudies Abbs";

        cellNames = @"Palaudies Abbs";


    }
    if (select == 2) {

        cellNames = @"Custom";

        [self.habitField resignFirstResponder];
        [self.habitField setInputView:nil];
        [self.habitField becomeFirstResponder];
        [toolBar setItems:@[backButton, flexSpace, barDoneButton] animated:YES];
        self.habitField.text = nil;
        self.habitField.placeholder = @"Custom";
        [self.delegate setCellName2:cellNames];

    }
}

@end

HabitViewController.h

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface HabitViewController : UITableViewController <DetailViewDelegate> {
}

@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 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;
    NSLog(@"%@", cellName);
    [myCells replaceObjectAtIndex:selectedRow withObject:cellName];
    [self.tableView reloadData];
}

@end

1 个答案:

答案 0 :(得分:0)

看起来在文本编辑开始时调用委托而不是在结束时调用委托。在编辑完成之前,cellName参数中没有任何有价值的东西。

在自定义的情况下,您必须使textField成为第一个响应者,然后在用户完成编辑时做出反应。

要捕获该事件,请将DetailVC作为文本字段的委托,并回复textFieldDidEndEditing。在该方法中,使用textField.text调用您的委托。