我有一个HabitViewController
(UITableViewController)
,其中包含一个添加单元格的按钮。添加单元格时,其默认标题为“New Habit”。然后用户可以点击单元格并出现带有选择器的detailViewController以选择习惯。然后将cell.label.text设置为选择器中的选定选项。这就是我的问题所在。例如,如果我通过按三次按钮添加3个单元格,则选择第三行。然后我选择了Hello World选项。顶部单元格将命名为hello world,而不是第三个单元格。这是正常的吗?这是我的代码:
#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
#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
#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
#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);
*/ //}
}
答案 0 :(得分:2)
你对此的看法是不对的。您不应该考虑设置单元格的标题,而是考虑更新用于使用数据填充表格视图的数组。这是您的应用的简化版本。单击“添加”按钮时,它会在第0行添加一个新单元格,其标签的文本为“New Habit”。当您单击该单元格(或任何其他单元格)时,它会将您带到具有选择器视图的控制器,您可以在其中选择字符串,并将该字符串传递回委托方法中的表视图。在该方法中,我更新数组,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]];
}
答案 1 :(得分:1)
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;