現在,我有一個按鈕添加更多tableViewController。點擊按鈕時,出現一個新的單元格,標題爲「新習慣」。當單擊該單元格時,DetailViewController將顯示一個文本字段。當輕擊文本字段時,UIPicker會顯示三個選項。第一個是「姿勢」,第二個是「Palaudies Abbs」,第三個選項是自定義,當選擇自定義時,鍵盤被替換爲鍵盤。使用鍵盤輸入自定義習慣,並將其顯示在文本框中。當選擇姿勢或palaudies時,tableViewCell被命名爲選項(您可以在代碼中看到它)。現在問題是將自定義選項設置爲單元格的標題。目前,我已經能夠接受字符串,並試圖將其設置爲與其他人一樣的刪除,但最終將標題設置爲無效。設置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
對不起,但我很困惑。我沒有看到這可以如何處理「Palaudies」文本,也不知道如何在不點擊添加按鈕的情況下使用'setCellName2:'方法。你的代碼還有什麼更多的東西是你遺漏的嗎? –
它在那裏,那就是我擁有的所有代碼。選擇選取器選項後,它會發送選定的選項。 –