为什么我不能从UITableViewCell的子类初始化UITextField?

时间:2013-03-23 03:50:22

标签: ios objective-c storyboard uitableview

我为iPad应用创建了UITableViewCell的子类。我需要动态生成文本字段,从用户获取输入,然后将该信息存储在数组中。我想问UITableViewCell UITextField.text对象,它将保存用户在我的View Controller的segue之前编写的内容(我在调用segue时保存NSString个对象)。所以我有一个UITableViewCells数组,我要求UITextField。文本对象。但由于某些原因,在创建UITableViewCell子类时,我的UITextField不是。我可以致电UITableViewSubclass并将其初始化,但UITableViewSubclass.UITextField为零。

这是我的UITableViewCell子类标题(是的,故事板中已连接UITextField):

#import <UIKit/UIKit.h>

@interface ConditionCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UITextField *condition;

@end

这是我的实施文件:

#import "ConditionCell.h"

@implementation ConditionCell
@synthesize condition;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.condition = (UITextField *)[self viewWithTag:10];
    }
    return self;
}

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

    // Configure the view for the selected state
}

@end

这是表视图控制器处理包含单元格的表:

.h文件:

#import <UIKit/UIKit.h>
#import "ConditionCell.h"

@interface ConditionsTableViewController : UITableViewController

@property (strong, nonatomic) NSMutableArray *conditionCellArray;

- (void)addNewConditionCell;

@end

.m文件:

#import "ConditionsTableViewController.h"

@interface ConditionsTableViewController ()

@end

@implementation ConditionsTableViewController

@synthesize conditionCellArray = _conditionCellArray;


- (NSMutableArray *)conditionCellArray
{
    if (_conditionCellArray == nil) {
        // Create the array object
        _conditionCellArray = [[NSMutableArray alloc] init];
    }

    return _conditionCellArray;
}

- (void)addNewConditionCell
{
    ConditionCell *condCell = [[ConditionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conditionCell"];

    [self.conditionCellArray addObject:condCell];


    [self.tableView beginUpdates];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.conditionCellArray.count-1 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];

    [self.tableView endUpdates];
    [self.tableView reloadData];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.conditionCellArray.count;
}

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

    if (cell == nil) {
        cell = [[ConditionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    //cell.condition = (UITextField *)[cell viewWithTag:1];

    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

此表视图控制器位于UIView控制器内,因为表视图不占用整个屏幕。当用户按下“确定”按钮时会触发一个segue,我在这里向Table View Controller询问包含UITableViewCells的数组,然后我通过foreach运行它来获取他们的.text属性。不幸的是,我似乎无法在文本字段中输入任何内容,因此.text的内容总是为零。如果有人能帮我解决这个问题,我们将不胜感激!

2 个答案:

答案 0 :(得分:0)

您可能会发现使用免费的Sensible TableView框架更容易。该框架具有开箱即用的这些文本字段单元格,甚至可以从您的阵列自动创建它们。

答案 1 :(得分:0)

我想出了一个更好的方法来做我想做的事情。事实证明,iOS的UITableView的工作方式与我想要做的完全不同。 UITableView通过查看故事板并给出单元格的标识符来工作,它创建它们并允许您在cellForRowAtIndexPath方法中设置它们的属性。但是,当单元格离开屏幕时,它不会保留为它自己的单独对象;它被重用了。因此,您可以将其视为滚动表格视图时,消失到一端的单元格会在另一端重新显示新信息。这是关键 - UITableView希望您提供单元格的信息。它不是直接在UITableViewCell上输入信息,这是我想要做的。

所以我最终做的是将我的单元格复制粘贴到他们自己的.xib文件中,并在子类initWithStyle:reuseIdentifier方法中执行:

NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"ConditionCell" owner:self options:nil];
        self = [nibArray objectAtIndex:0];

然后用你想要的任何样式 - 设置 - UI元素创建单元格。

接下来,我想继续对单元格的引用,因为该单元格有一个文本框,当用户按下“完成”按钮时,我需要保存文本框中的内容。但是,测试揭示了我上面解释的重用问题。那怎么做?在我的Table的视图控制器中,每当用户想要添加一个新文本框(并按下按钮来执行此操作)时,我有一个方法可以

[self.conditionCellArray insertObject:[[ConditionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conditionCell"] atIndex:0];

这会向数组添加一个新单元格 - 这很重要,因为我需要始终引用所有单元格。 (它在索引0处添加单元格,因为我想将其插入顶部)。然后,在cellForRowAtIndexPath方法中,我做了

return [self.conditionCellArray objectAtIndex:indexPath.row];

将返回相应的单元格。请记住,从我所读到的,关于保持对表格中每个单元格的引用的整个事情与Apple在使用UITableView时所述的最佳实践相反。但是,正如我之前所说,UITableView旨在显示信息,而不是从用户输入中收集信息。所以这就是为什么我必须打破规则,如果你愿意的话,达到预期的效果(我想要的)。我希望这能帮助那些希望做同样事情的人;如果有更好的方法,不要害羞告诉我。

编辑:哦顺便说一句,当您将在storyboard中创建的单元格粘贴到他们自己的.xib文件时,请确保断开所有IBOutlet并将其类更改回UITableViewCell。这样,连接.xib文件单元时不会出现任何问题或冲突。

相关问题