2012-06-26 46 views
0

我有3个表格单元格,不知何故它显示了最后一个单元格中的最后两个内容。我在代码中的某个地方做了一些错误,但我不知道在哪里,因为我只是按照教程进行操作,并尝试像教程中那样做,但不是2个单元我想要3个可编辑单元格。两个内容被写入一个可编辑单元格

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 (...))有人比我更了解它吗?

+0

感谢所有的快速解答。我试图用'elsif'(Ruby-syntax)做同样的事情,并想知道为什么它没有成功。也忘了'indexPath.row'的东西。 – Kirinriki

回答

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]; 
} 

您可以在之后如果添加其他IFS的任何款项。

1

你说得对错误的来源。您正在以相同的坐标添加authorField & atextField。包括3个原因if S上的正确的方法是:

if (/* condition 1 */) { 

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

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

} 
+0

啊呀!我一直在用Ruby语法的思维方式(elsif)......这就是为什么它没有成功。 – Kirinriki

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]; 
} 
} 
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]; 
} 
相关问题