0

テーブルセルが3つあり、どういうわけか最後のセルに最後の2つのコンテンツが表示されます。コードのどこかで何か間違ったことをしましたが、チュートリアルに従って、チュートリアルと同じようにしようとしているので、どこにあるのかわかりませんが、2つのセルの代わりに3つの編集可能なセルが必要です。

ここに画像の説明を入力してください

完全なコード:

#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 (...))誰かが私よりもそれをよく知っていますか?

4

4 に答える 4

1

あなたはエラーの原因について正しいです。同じ座標でauthorField&を追加しています。sのatextField3つの原因を含める正しい方法は次のとおりです。if

if (/* condition 1 */) {

}
else if ( /* condition 2 */) {

}
else if ( /* condition 3 */) {

}
于 2012-06-26T08:44:56.203 に答える
1

prepareCell:forIndexPathで、両方のサブビューを最後のセルに追加します。説明したとおりにelseifを使用できるため、メソッドは次のようになります。

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

ifの後にelseifをいくつでも追加できます。

于 2012-06-26T08:45:11.747 に答える
1

これを行う:

- (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0)  {
    [cell.contentView addSubview:titleField];   
} else if(indexPath.row == 1) { 
    [cell.contentView addSubview:authorField];
}else if(indexPath.row == 2) { 
    [cell.contentView addSubview:atextField];
}
}
于 2012-06-26T08:45:41.263 に答える
1
if (indexPath.row == 0)  {
    [cell.contentView addSubview:titleField];   
} else if(indexPath.row == 1) { 
    [cell.contentView addSubview:authorField];
}else if(indexPath.row == 2) { 
    [cell.contentView addSubview:atextField];
}
于 2012-06-26T08:46:41.887 に答える