Reputation: 629
I am having a heck of time with this. Using storyboard, I created a table view controller with a static cell that contains a UITextField to allow for user input. When the user is finished, I want to retrieve the contents of the text field.
Here is what I did:
UITableViewCell
named SingleLineFieldTableViewCell
IBOutlet UITextField *textField;
to the subclass and declared it as a property (nonatomic, retain) and synthesized it.Added IBOutlet SingleLineFieldTableViewCell *cellNamed;
to the owning table view controller, and declared it as a property (nonatomic, retain) and synthesized it.
In storyboard, I have a table view controller with static cells. One of the cells is the custom cell, which is declared as SingleLineFieldTableViewCell
and owns a UITextField
. It is also assigned a cell identifier.
When I run, dequeueReusableCellWithIdentifier
returns nil
. I thought that with Xcode 4 and storyboards, dequeueReusableCellWithIdentifier
, according to Converting to Storyboards Release Notes, "The dequeueReusableCellWithIdentifier:
method is guaranteed to return a cell (provided that you have defined a cell with the given identifier)".
The weird part is that when I run in the Simulatior, the table appears as expected (section, cell size, etc.), except that I can't edit the custom cell.
I'm at a loss. Any help or ideas?
--John
Upvotes: 9
Views: 10773
Reputation: 71
According to the Apple's docs (Populating a Static Table View With Data) http://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW31
Note: If a table view in a storyboard is static, the custom subclass of
UITableViewController
that contains the table view should not implement the data source protocol. Instead, the table view controller should use itsviewDidLoad
method to populate the table view’s data.
Thus you just need to remove all the Table View data source methods from your View Controller.
Optional:
However if your View Controller is also the data source for other dynamic Table View(s) and you still need these methods, it's possible to call only the super
's corresponding data source methods for the static Table View:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Static Table View
if (tableView == self.tableView)
return [super numberOfSectionsInTableView:tableView];
// Dynamic Table View
// ...
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Static Table View
if (tableView == self.tableView)
return [super tableView:tableView numberOfRowsInSection:section];
// Dynamic Table View
// ...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Static Table View
if (tableView == self.tableView)
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
// Dynamic Table View
// ...
}
Upvotes: 0
Reputation: 1338
I know this question was a year ago, but I just went through this problem myself today.
I think the problem here is using static cells.
See the Apple docs: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html
Basically the idea is that if you are using static cells then there is no need to use
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
data source method, instead just generate outlets to all your UI elements (inside the static cells) and set them directly.
If you need to use this method then you must change the table to dynamic content mode.
Upvotes: 6
Reputation: 122391
I am a newbie too, so this might be complete crap, but I would have told the UITextLabel
to call one of my methods when the user had finished editing and not worried about trying to deque it from the Table View:
- (IBAction)userFinishedEditing:(id)sender
{
...
}
- (void) someMethod
{
...
UITextLabel *label = ...;
[label addTarget:self action:@selector(userFinishedEditing:sender:) forControlEvents:
UIControlEventEditingDidEnd];
...
}
Upvotes: 0
Reputation: 6325
Are you building for iOS 5 or 4?
If you are trying with 4.x it won't crash as the method is valid, but doesn't return the cell. I have had no problem setting it up with custom classes. here is my entire method:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
GameDetailCell* cell=[tableView dequeueReusableCellWithIdentifier:@"gameCell"];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
my storyboard looks like:
Upvotes: 3
Reputation: 582
Alert.m Class in which we used custom cell..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"mycell";
AlertCustomCell *cell = (AlertCustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[[AlertCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.lAlert.text=[[alertArray objectAtIndex:indexPath.section] objectForKey:@"alertName"];
cell.lblDate.text=[[alertArray objectAtIndex:indexPath.section] objectForKey:@"date"];
UIImageView*imgview=[[UIImageView alloc]initWithFrame:CGRectMake(-28, 0, 275, 60)];
imgview.image=[UIImage imageNamed:@"strip_s14.png" ];
UIImageView*selimgview=[[UIImageView alloc]initWithFrame:CGRectMake(-28, 0, 275, 60)];
selimgview.image=[UIImage imageNamed:@"strip_s14_h.png" ];
[cell setSelectedBackgroundView:selimgview];
cell.backgroundView = imgview;
cell.backgroundColor=[UIColor clearColor];
return cell;
}
AlertCustomCell.m
#import "AlertCustomCell.h"
@implementation AlertCustomCell
@synthesize lblAlert,lblDate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
Alert=[[UITextField alloc]initWithFrame:CGRectMake(10, 18, 80, 21)];
Alert.backgroundColor=[UIColor clearColor];
Alert.text=@"Alert 1";
Alert.font=[UIFont fontWithName:@"Arial-BoldMT" size:15.0];
[self.contentView addSubview:lblAlert];
lblDate=[[UILabel alloc]initWithFrame:CGRectMake(70, 18, 150, 21)];
lblDate.backgroundColor=[UIColor clearColor];
lblDate.text=@"july 12,2011 4:17 PM";
lblDate.font=[UIFont fontWithName:@"ArialMT" size:15.0];
[self.contentView addSubview:lblDate];
}
return self;
}
Upvotes: -2