更改任何单元名称时,顶部单元名称会更改 [英] Top cell name changes when changing any cell name

查看:87
本文介绍了更改任何单元名称时,顶部单元名称会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a HabitViewController (UITableViewController) with a button to add cells. When a cell is added its default title is "New Habit". Then the user can tap the cell and a detailViewController appears with a picker to choose the habit. The cell.label.text is then set to the selected option in the picker. This is where my problem is. For example, if i added 3 cells by pressing the button three times, then selected the third row. And then I chose the option Hello World. The top cell would be named hello world, not the third cell. Is this normal? Here is my code:

#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



.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



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



.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;

这篇关于更改任何单元名称时,顶部单元名称会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆