[英]Can't return a value from UITextField to UILabel in UITableViewCell as subviews
[英]Why can't I initialize a UITextField from a subclass of UITableViewCell?
我为iPad应用程序创建了UITableViewCell
的子类。 我需要动态生成文本字段,从用户那里获取输入,然后将该信息存储在数组中。 我想到要向UITableViewCell
请求UITextField.text
对象,该对象将保存用户在View Controller进行Segue之前写的所有内容(在调用Segue时保存NSString
对象)。 因此,我有一个UITableViewCells数组,我需要UITextField
.text对象。 但是由于某种原因,在创建UITableViewCell
子类时,没有创建UITextField
。 我可以调用UITableViewSubclass
并将其初始化,但是UITableViewSubclass.UITextField
为nil。
这是我的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
这是Table View Controller,用于处理包含单元格的表:
.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控制器内部,因为表视图不会占据整个屏幕。 当用户按下“确定”按钮时,会触发一个选择,正是在这里我向此表视图控制器询问包含UITableViewCells
的数组,然后我通过一个foreach来获取其.text属性。 不幸的是,我似乎无法在文本字段中输入任何内容,因此.text始终为零。 如果有人可以帮助我解决这个问题,将不胜感激!
使用免费的Sensible TableView框架,您可能会发现这样做更容易。 该框架具有开箱即用的这些文本字段单元格,甚至可以从您的数组自动创建它们。
我想出了一种更好的方法来做自己想做的事。 事实证明,iOS的UITableView的工作方式与我想要的完全不同。 UITableView通过查看情节提要和给定的单元格标识符来工作,创建它们并允许您在cellForRowAtIndexPath方法内设置其属性。 但是,当单元离开屏幕时,它不会保留,因为它是自己的单独对象。 它被重用。 因此,您可以认为它就像在滚动表格视图时,消失到一端的单元格在另一端再次出现新信息。 这是关键-UITableView希望您提供单元格的信息。 我不是想直接在UITableViewCell上输入信息。
因此,我最终要做的是将单元格复制粘贴到自己的.xib文件中,并在子类initWithStyle:reuseIdentifier方法中执行以下操作:
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"ConditionCell" owner:self options:nil];
self = [nibArray objectAtIndex:0];
这样就可以创建具有任何样式的单元格-设置-所需的UI元素。
接下来,我要保留对该单元格的引用,因为该单元格具有一个文本框,并且当用户按下“完成”按钮时,我需要保存该文本框上的内容。 但是,测试发现了我上面解释的重用问题。 那么该怎么做呢? 在我的表格的视图控制器中,每当用户想要添加一个新的文本框(并按下按钮来这样做)时,我都有一个方法
[self.conditionCellArray insertObject:[[ConditionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conditionCell"] atIndex:0];
这会向数组中添加新的单元格-这很重要,因为我需要始终引用所有单元格。 (它正在将单元格添加到索引0,因为我想将其插入顶部)。 然后,在cellForRowAtIndexPath方法中,我做了
return [self.conditionCellArray objectAtIndex:indexPath.row];
它将返回相应的单元格。 请记住,从我读到的所有有关保持对表中每个单元格的引用的内容来看,这与苹果公司使用UITableView时所说的最佳做法背道而驰。 但是,正如我之前说的,UITableView用于显示信息,而不是从用户输入中收集信息。 因此,这就是为什么我必须违反规则才能达到预期的效果(我想要的)。 我希望这对其他希望做同样事情的人有所帮助; 如果有更好的方法,请不要羞于告诉我。
编辑:哦,顺便说一句,当您将在情节提要中创建的单元格复制粘贴到其自己的.xib文件中时,请确保断开所有IBOutlet的连接并将其类更改回UITableViewCell。 这样,在连接.xib文件单元时就不会有任何问题或冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.