gpt4 book ai didi

objective-c - 在一个可编辑单元格中写入两个内容

转载 作者:行者123 更新时间:2023-11-28 19:17:40 25 4
gpt4 key购买 nike

我有 3 个表格单元格,它以某种方式在最后一个单元格中显示了最后两个内容。我在我的代码中某处做错了,但我不知道在哪里,因为我只是按照教程并尝试像教程中那样做同样的事情,但我想要 3 个可编辑单元格而不是 2 个单元格。

enter image description here

完整代码:

#import "LocationAddViewController.h"
#import "Location.h"

@interface LocationAddViewController ()
- (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath;
- (UIBarButtonItem *)newCancelButton;
- (UIBarButtonItem *)newSaveButton;
- (UITextField *)newTextField;
@end

@implementation LocationAddViewController

@synthesize location;
@synthesize titleField;
@synthesize authorField;
@synthesize atextField;
@synthesize delegate;


- (void)dealloc {
[location release];
[titleField release];
[authorField release];
[atextField release];
[super dealloc];
}


- (id)initWithLocation:(Location *)aLocation andDelegate:(id)aDelegate {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
self.location = aLocation;
self.delegate = aDelegate;
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

self.tableView.allowsSelection = NO;

titleField = [self newTextField];
titleField.keyboardType = UIKeyboardTypeASCIICapable;
[titleField becomeFirstResponder];

authorField = [self newTextField];
authorField.keyboardType = UIKeyboardTypeASCIICapable;

atextField = [self newTextField];
atextField.keyboardType = UIKeyboardTypeASCIICapable;

if (location.onelocationId) {
titleField.text = location.title;
authorField.text = location.author;
atextField.text = location.text;
} else {
titleField.placeholder = @"Title";
authorField.placeholder = @"Author";
atextField.placeholder = @"Text";
}

UIBarButtonItem *cancelButton = [self newCancelButton];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];

UIBarButtonItem *saveButton = [self newSaveButton];
self.navigationItem.rightBarButtonItem = saveButton;
saveButton.enabled = NO;
[saveButton release];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

if (location.onelocationId) {
self.title = @"Edit Location";
} else {
self.title = @"Add Location";
}
}


-(IBAction)cancel {
[self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)save {
location.title = titleField.text;
location.author = authorField.text;
location.text = atextField.text;

[self.delegate didChangeLocation:location];
[self.navigationController popViewControllerAnimated:YES];
}



- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return 3;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell =
[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil] autorelease];

[self prepareCell:cell forIndexPath:indexPath];

return cell;
}



- (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
[cell.contentView addSubview:titleField];
} else {
[cell.contentView addSubview:authorField];
[cell.contentView addSubview:atextField];
}
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
if (textField == titleField) {
[authorField becomeFirstResponder];
}
if (titleField == authorField) {
[self save];
}
return YES;
}

- (IBAction)textFieldChanged:(id)sender {
BOOL enableSaveButton =
([self.titleField.text length] > 0) && ([self.authorField.text length] > 0) && ([self.atextField.text length] > 0);
[self.navigationItem.rightBarButtonItem setEnabled:enableSaveButton];
}

- (UIBarButtonItem *)newCancelButton {
return [[UIBarButtonItem alloc]
initWithTitle:@"Cancel"
//auch im Original gelb
style:UIBarButtonSystemItemCancel
target:self
action:@selector(cancel)];
}

- (UIBarButtonItem *)newSaveButton {
return [[UIBarButtonItem alloc]
initWithTitle:@"Save"
//auch im Original gelb
style:UIBarButtonSystemItemSave
target:self
action:@selector(save)];
}

- (UITextField *)newTextField {
UITextField *textField =
[[UITextField alloc] initWithFrame:CGRectMake(10, 10, 285, 25)];
textField.font = [UIFont systemFontOfSize:16];
textField.delegate = self;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
[textField addTarget:self
action:@selector(textFieldChanged:)
forControlEvents:UIControlEventEditingChanged];
return textField;
}

@end

我想问题出在这里:

    - (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
[cell.contentView addSubview:titleField];
} else {
[cell.contentView addSubview:authorField];
[cell.contentView addSubview:atextField];
}
}

我对整个编程并不太感兴趣,但我想我必须写 3 个 if 子句? (类似于 if (...) elsif (...) else (...))有人比我更了解它吗?

最佳答案

在您的 prepareCell:forIndexPath 中,您将两个 subview 添加到最后一个单元格中。您可以按照您的描述使用 elseif,这样您的方法看起来像这样

if (indexPath.row == 0)  {
[cell.contentView addSubview:titleField];
} else if (indexPath.row == 1) {
[cell.contentView addSubview:authorField];
} else {
[cell.contentView addSubview:atextField];
}

您可以在 if 之后添加任意数量的 else if。

关于objective-c - 在一个可编辑单元格中写入两个内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11203623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com