我爲iPad應用程序創建了UITableViewCell
的子類。我需要動態生成文本字段,從用戶處獲取輸入,然後將該信息存儲在數組中。我想要問UITableViewCell
的UITextField.text
對象,它可以容納用戶在我的View Controller的segue之前寫入的內容(我在調用segue時保存NSString
對象)。所以我有一個UITableViewCells數組,我要求輸入UITextField
.text對象。但由於某種原因,當我的UITableViewCell
子類正在創建時,我的UITextField
不是。我可以撥打UITableViewSubclass
並初始化,但UITableViewSubclass.UITextField
爲零。爲什麼我無法從UITableViewCell的子類中初始化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控制器內的表視圖不佔用整個屏幕。當用戶按下'ok'按鈕時,會觸發一個segue,在這裏我要求這個Table View Controller包含UITableViewCells
的數組,然後我通過foreach運行它以獲取它們的.text屬性。不幸的是,我似乎無法獲得任何我輸入到文本字段的內容,因此.text的值始終爲零。如果有人可以幫助我解決這個問題,將不勝感激!
在您的第一個.m文件中,如果故事板中連接了「condition」的IBOutlet,爲什麼要在初始化方法中設置其值? – Isaac 2013-03-23 03:57:56
如果你把你的單元放在IB中,爲什麼要使用initWithStyle ...?另外我敢打賭,保持一個表格單元陣列不是你想要做的。你可能想要一個字符串數組。 – 2013-03-23 05:41:17
initWithStyle現在正在給我帶來很多痛苦。我應該把一個init方法放在單元的.m文件中,然後調用它嗎? – Tino 2013-03-23 14:21:07