0

我希望那里的人可以帮助我了解我要去哪里错了

我有一个 TableViewController,为了保存 tableview 的内容,我更改了 -(void)viewDidLoad 代码以添加一个保存按钮...

- (void)viewDidLoad
{
  [super viewDidLoad];

  self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
                                           target:self 
                                           action:@selector(insertCustomData)
                                           ];

但是,在我的insertCustomData函数中,我需要像在控制器代码中的其他地方一样访问 tableview。我需要访问我的表格视图,以便在保存到数据库之前验证单元格值。我可以看到如何从工具栏定义中传递对 tableview 的引用(如果应该这样做的话)

我什至不确定我是否应该在这个阶段访问 tableview 单元格的值(我正在尝试构建单元格数据的 NSObject 以传递给我编写的数据控制器中的验证函数,以验证并保存到 SQLite 数据库中)

抱歉,如果这看起来微不足道,但现在我坚持这一点,任何帮助将不胜感激

谢谢

[更新]

我将 tableView 用于数据输入屏幕,因此最初没有要显示的数据。tableView 在 tableview 控制器的范围内,但由于我需要这样做,我在运行时使用自定义单元格定义创建单元格(在 .xib 文件中,具有关联的 .h 和 .m 文件) . 自定义单元格定义只是一个 UILabel 和一个 UITextField(用于数据输入)

这些字段具有占位符以指示需要输入的内容,否则为空。我确实考虑过是否仍需要将 tableView 基于数组或字典对象来捕获用户输入

我写这个的类有定义@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>,因为它是从 UITableViewController 初始模板(或任何它调用的)创建的

我觉得我应该更早地捕获数据并且只从选择器中调用 save 方法?

我是否只需要使用 nil NSStrings 创建一个 NSArray 并将其分配给 tableView 中的每个 UITextField/Cell ?如果我这样做,文本条目会进入这个 NSArray 吗?

[已解决 - 答案如下]

我需要的是将标准 .h 文件修改为此

@interface CustomViewController : UITableViewController  <UITextFieldDelegate> {
  IBOutlet UITableView *MyTableView;
}

@property(nonatomic, retain) IBOutlet UITableView *MyTableView;

然后在界面生成器中,我能够将 tableview 链接到我的新声明。这是最重要的一步,因为这样可以确保我能够修改动态构建单元格的主要代码而不是通用 tableView 以引用它,如下所示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"EditableCustomCell";
  EditableCustomCell *cell = [[self MyTableView] dequeueReusableCellWithIdentifier:CellIdentifier];

我还在synthesize MyTableView顶部添加了一个

这使得MyTableView我需要编写的任何新方法都可以访问它,从而使从 leftBarButtonItem 选择器访问它变得insertCustomData轻而易举

非常感谢Kimpoy在这方面的帮助!

(注意:具有讽刺意味的是,在完成此操作后,查看我发布的上一个问题,我应该从我之前关于 segues 和 tableviews 的问题中学到如何在 segue 期间从视图控制器引用 tableView

4

2 回答 2

1

在 AddViewController.h 文件中:

@interface AddViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> {
    UITableView             *tableViewInfo;
    UITextField             *txtFieldUsed;
}

@property (nonatomic, retain) IBOutlet UITableView *tableViewInfo;
@end

在 AddViewController.m 文件中:

@implementation AddViewController

@synthesize tableViewInfo; // Synthesize your tableViewInfo property

// Called to get the text values from the textfields in the table view cells/rows
- (NSDictionary *)getValueForTextField:(UITableView *)tableView {
    NSMutableDictionary *mutDictCredential = [[[NSMutableDictionary alloc] init] autorelease];
    for (int row = 0; row < 2; row++) {
        NSIndexPath *idxCell = [NSIndexPath indexPathForRow:row inSection:0];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:idxCell];
        for (UITextField *textField in cell.contentView.subviews) {
            if ([textField isKindOfClass:[UITextField class]] && row == textField.tag) {
                NSString *strText = textField.text;
                if (!strText) {
                strText = @"";
                }
                if (row == 0) {
                [mutDictCredential setObject:strText forKey:@"CoffeeName"];
                }
                else if (row == 1) {
                [mutDictCredential setObject:strText forKey:@"Price"];
                }
            }
        }
    }
    return mutDictCredential;
}

#pragma mark -
#pragma mark Tableview datasource

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [[self tableViewInfo] dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        CGRect rect = [cell frame];
        // Create editable textfield within cell or row
        UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(rect.origin.x + 10, rect.origin.y, rect.size.width - 40, rect.size.height)];
        [txtField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [txtField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [txtField setTextAlignment:UITextAlignmentLeft];
        [txtField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
        [txtField setTag:indexPath.row];
        [txtField setDelegate:self];

        // Set cells texts
        switch ([txtField tag]) {
          case 0:
            [txtField setPlaceholder:@"Coffee Name"];
            [txtField setKeyboardType:UIKeyboardTypeDefault];
            [txtField setReturnKeyType:UIReturnKeyNext];
            break;
          case 1:
            [txtField setPlaceholder:@"Price"];
            [txtField setKeyboardType:UIKeyboardTypeDecimalPad];
            break;
          default:
            break;
        }
        [[cell contentView] addSubview:txtField];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [txtField release];
     }
    return cell;
}

#pragma mark -
#pragma mark Tableview delegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20.0;
}

// This can be your insertCustomData
- (void) save_Clicked:(id)sender {
    // Get data from table
    [self getValueForTextField:[self tableViewInfo]];

    NSMutableDictionary *tempMutDictInfo = [[NSMutableDictionary alloc] initWithDictionary:[self getValueForTextField:[self tableViewInfo]]];

    NSString *paramName = [tempMutDictInfo valueForKey:@"CoffeeName"];
    NSString *paramMessage = [tempMutDictInfo valueForKey:@"Price"];
    [tempMutDictInfo release];

    NSMutableDictionary *mutDictInfo = [[NSMutableDictionary alloc] init];
    [mutDictInfo setObject:paramName == nil ? @"" : paramName forKey:@"CoffeeName"];
    [mutDictInfo setObject:paramMessage == nil ? @"" : paramMessage forKey:@"Price"];

    // Database
    SQLAppDelegate *appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];

    //Create a Coffee Object.
    Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:0];
    coffeeObj.coffeeName = [mutDictInfo valueForKey:@"CoffeeName"];
    NSDecimalNumber *temp = [[NSDecimalNumber alloc] initWithString:[mutDictInfo valueForKey:@"Price"]];

    [mutDictInfo release];

    coffeeObj.price = temp;
    [temp release];
    coffeeObj.isDirty = NO;
    coffeeObj.isDetailViewHydrated = YES;

    //Add the object
    [appDelegate addCoffee:coffeeObj];

    //Dismiss the controller.
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
    [tableViewInfo release];
    [super dealloc];
}

@end

您可以将此作为参考。

于 2012-06-13T19:00:49.570 回答
0

我会为我的视图控制器分配 2 个附加属性。1 用于我的表格视图,1 用于我的数据源。

于 2012-06-13T14:43:25.827 回答