Custom UITableViewCell redraw issues - iphone

I have a custom UITableView cell that I've added a textbox to for editing, that shows and hides based on the edit mode. I've also tried adding a vertical line that shows when editing, and it does that, but I'm running into some drawing issues. I just added a green checkmark rightView to start working on input validation feedback, and I'm seeing similar issues.
Here is the code for the cell, and part of my cellForRowAtIndexPath.
#import <UIKit/UIKit.h>
#interface EditableCellStyle2 : UITableViewCell {
CGRect editRect;
UITextField *editField;
UIView *lineView;
}
#property (nonatomic, readonly, retain) UITextField *editField;
#property (nonatomic, readonly, retain) UIView *lineView;
#end
#import "EditableCellStyle2.h"
#implementation EditableCellStyle2
#synthesize editField;
#synthesize lineView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
editRect = CGRectMake(83, 12, self.contentView.bounds.size.width-83, 19);
editField = [[UITextField alloc] initWithFrame:editRect];
editField.font = [UIFont boldSystemFontOfSize:15];
editField.textAlignment = UITextAlignmentLeft;
editField.textColor = [UIColor blackColor];
editField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:editField];
self.editField.enabled = NO;
self.editField.hidden = YES;
lineView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 1, self.contentView.bounds.size.height)];
self.lineView.backgroundColor = [UIColor lightGrayColor];
[self.contentView addSubview:lineView];
self.lineView.hidden = YES;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
-(void)layoutSubviews
{
[super layoutSubviews]; // layouts the cell as UITableViewCellStyleValue2 would normally look like
editRect = CGRectMake(83, 12, self.contentView.frame.size.width-self.detailTextLabel.frame.origin.x-10, 19);
editField.frame = editRect;
}
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if (state & UITableViewCellStateEditingMask) {
self.detailTextLabel.hidden = YES;
self.editField.enabled = YES;
self.lineView.hidden = NO;
self.editField.hidden = NO;
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
[super didTransitionToState:state];
if (!(state & UITableViewCellStateEditingMask)) {
self.editField.enabled = NO;
self.editField.hidden = YES;
self.lineView.hidden = YES;
self.detailTextLabel.hidden = NO;
self.editField.text = self.detailTextLabel.text;
}
}
- (void)dealloc {
[editField release];
[lineView release];
[super dealloc];
}
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// handling every section by hand since this view is essentially static. Sections 0, 1, 2, and 4 use a generic editable cell.
// Section 3 uses the multiline address cell.
static NSString *CellIdentifier = #"Cell";
EditableCellStyle2 *cell = (EditableCellStyle2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section == 0 || indexPath.section == 1 || indexPath.section == 2 || indexPath.section == 4) {
if (cell == nil) {
cell = [[[EditableCellStyle2 alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
}
}
// Configure the Odometer
if (indexPath.section == 0) {
NSArray *array = [sectionsArray objectAtIndex:indexPath.section];
NSDictionary *dictionary = [array objectAtIndex:indexPath.row];
cell.textLabel.text = #"Odometer";
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [dictionary objectForKey:#"Odometer"]];
cell.tag = kOdometer;
cell.editField.text = cell.detailTextLabel.text;
cell.editField.placeholder = #"Odometer";
cell.editField.tag = kOdometer;
cell.editField.keyboardType = UIKeyboardTypeNumberPad;
// Create a view for the green checkmark for odometer input validation and set it as the right view.
UIImage *checkImage = [UIImage imageNamed:#"tick.png"];
UIImageView *checkImageView = [[[UIImageView alloc] initWithImage:checkImage] autorelease];
cell.editField.rightView = checkImageView;
cell.editField.rightViewMode = UITextFieldViewModeAlways;
}
return cell;
}
There is more to it but all the cells are built the same way.
The problems are that, when in edit mode, the vertical lines will display properly. When I leave edit mode, any cells that were off screen when I go to normal mode still have the vertical line (it doesn't get hidden). Also, now that I've added the imageView for the checkmark indicator, any cells that are off screen when switching modes gain the checkmark. (only section 0 sets it up).
I've also noticed that if i do cell.setNeedsDisplay, the text label and detail text label won't update if the data source has been updated. I have to do [self.tableView reloadData] which skips any active animations.
I'm sure these issues are related to me using a custom cell + dequeueReusableCellWithIdentifier, but I can't find exactly what.
Any feedback or a push in the right direction would be appreciated.
Edit:
Not using reusable cells seems to have resolved the above issues. I'm still open to feedback on the cell code.
I forgot one other issue that may or may not be related. One of my cells has a "tap to view list" button. If I enter data into the cells while in edit mode, then hit that button to choose some info from a list (it displays a modal table view), when I dismiss the modal view, all of the cells' edited data has reverted to their original state. I'm not calling reload data when I dismiss the modal view controller. I thought this might be fixed by not using reusable cells but it isn't.

You need to prepare the cell for reuse. Try adding this to the EditableCellStyle2 implementation:
- (void)prepareForReuse {
[super prepareForReuse];
[self didTransitionToState:UITableViewCellStateDefaultMask];
}

Maybe you trimmed too much for your post, but in the posted code your reusable cell handling is all wrong.
First of all, each different type of cell needs its own CellIdentifier. In your case (judging from your code comment), that means at least a different identifier for section 3 versus sections 0, 1, 2, and 4. You may also want to do a separate identifier for section 0, so you don't have to keep removing and readding that checkmark. The different identifier needs to be used for both the dequeueReusableCellWithIdentifier: and initWithStyle:reuseIdentifier:` for the appropriate sections.
The second problem is that you are not resetting the cells correctly. There are two "kinds" of initialization that must be done to a UITableViewCell: initialization that is the same for every cell of its type, and initialization that depends on the specific row being displayed. The first kind can (and should) only be done once, when a new cell is allocated. The second kind must be done every time through tableView:cellForRowAtIndexPath:. You seem to be doing the first correctly for your EditableTableCell2 class in its init method, but I see nowhere in there where you do the per-row initialization: you never reset selected, or the cell state, or the contents of the edit field, or remove the checkImageView since you are using the same kind of cell for section 0 versus the other sections. If you want, the reset selected, state, and clearing out the checkbox image and field contents can be done in prepareForReuse on your EditableTableCell2 class.
The third problem, which is almost certainly due to over-trimming, is that you never create this "multiline address" cell for section 3. You'll end up maybe reusing a random EditableTableCell2, or maybe crashing on an exception from the framework when you return nil from tableView:cellForRowAtIndexPath:.

Related

Custom Cell, only last cell gets [layoutSubviews]?

I'm creating a Settings View for my app, and in that view is a UITableView. I'm creating custom cells to meet my needs, but I'm having issues - only the last cell is getting [layoutSubviews]. Am I doing something wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//int type = (indexPath.row == 0?1:0);
//if(indexPath.row == 6) type = 2;
NSLog(#"row %i created", indexPath.row);
TableCell *cell = [[TableCell alloc] initWithType:indexPath.row];
cell.textLabel.text = #"Test cell";
return cell;
}
And in my custom cell:
#implementation TableCell
UIImageView *shadowView;
int row;
- (id) initWithType:(int)type {
row = type;
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
self.backgroundColor = [UIColor clearColor];
self.backgroundView = [[UIView alloc] init];
UIImage *shadowImage = [UIImage imageNamed:#"CellShadow"];
shadowImage = [shadowImage resizableImageWithCapInsets:UIEdgeInsetsMake(14, 14, 14, 14)];
shadowView = [[UIImageView alloc] initWithImage:shadowImage];
[self.contentView addSubview:shadowView];
//[self.contentView sendSubviewToBack:shadowView];
NSLog(#"agreed, row %i created", row);
[self layoutSubviews];
return self;
}
- (void) layoutSubviews {
NSLog(#"row: %i", row);
[super layoutSubviews];
shadowView.frame = CGRectMake(
0, 0,
self.contentView.frame.size.width,
self.contentView.frame.size.height
);
}
#end
Continuously, only the last cell #6, is reported when I rotate, or when layoutSubviews should be called. Any suggestions?
Do not call layoutSubviews directly. Use [self setNeedsLayout] or [self layoutIfNeeded]. But do not call these at all in the cell's init method.
Also, do not call [[TableCell alloc] initWithType:indexPath.row]; directly, either. Instead, use...
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
Once you've built that cell, you can tell it it's row, but be aware that the cells get recycled as the table scrolls, so you must update that value on every call to cellForRowAtIndexPath.
The cells ought to get layout again (without you making any calls direct or indirect) when the table view is resized.
See the tableview doc here.
You should never call layoutSubviews directly, it will be called automatically by iOS once the cell is ready to display. You should also deque the cell as #danh is recommending. If you're not very comfortable with all this, then I'd really recommend you have a look at the free Sensible TableView framework, which automates creating these kind of settings views (I create mine in a couple of lines, really).
The issue was of my own poor code. Using cell.backgroundView helped a lot here.
Never Call layoutSubviews by yourself. It will be called when ever frames of subview in cell are changed. Even if just change the text of labels in your custom cell wont call layoutSubviews. Ue the deque of cells for reusing for better performance. As it wont allocate cell every time. And in you code looks like has lot of memory issues since cell allocated wont be released and new cell is created.

iOS: dynamically adding data from UITableView to NSArray

I have a UITableView in which I have a custom prototype cell, defined in another class (CustomCell), with a UITextField in it. Every time I press a button, it calls a method called addItem, which creates a new cell. I want the texts in the UITextFields to go to an array. To try to explain it better, if I add 3 cells to the UITableView and input 3 texts in the corresponding UITextFields, I want the text in 1st cell to go to the array in index 0, the text in the 2nd to go to index 1 and the text in 3rd cell to go to index 2. My biggest problem is that I want to be able to go back to UITextField in cell 1 and update it, and have it dynamically update the NSArray object corresponding to it, that is, the one at index 0. I have no idea how to approach it. Can anybody help???
Thank you very much!!
my code (obs: itemTable is the UITableView):
MainViewController.m
#implementation addViewController{
NSInteger n;
NSString *aid;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)viewWillAppear:(BOOL)animated{
n=1;
aid=#"";
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return n;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier= #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.itemNumber.text=[NSString stringWithFormat:#"Item %d",indexPath.row];
return cell;
}
- (IBAction)addItem:(UIButton *)sender {
++n;
[_itemTable reloadData];
}
- (IBAction)removeItem:(UIButton *)sender {
if (n>=0)--n;
[_itemTable reloadData];
}
CustomCell.m:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {_itemValue = [[UITextField alloc]init];
_item = [[UILabel alloc]init];
[self.contentView addSubview:_itemValue];
[self.contentView addSubview:_item];
}
return self;
}
CustomCell.h
#interface CustomCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *itemNumber;
#property (strong, nonatomic) IBOutlet UITextField *itemValue;
#end
First, when you create each text field, you make yourself that text field's delegate, so you will get messages whenever something happens in the text field.
Okay, so now when the user types in a text field, you will get messages, and you can modify your model (the array, which you should keep as an NSMutableArray I suppose). But to do that, you need to figure out which the heck cell contains the text field that this message is coming from! You will do that something like this:
- (void)textFieldDidEndEditing:(UITextField *)tf {
// some cell's text field has finished editing; which cell?
UIView* v = tf;
do {
v = v.superview;
} while (![v isKindOfClass: [UITableViewCell class]]);
CustomCell* cell = (CustomCell*)v;
// so which row is it?
NSIndexPath* ip = [self.tableView indexPathForCell:cell];
// aha! so now ip.row is the row, and you can update your data model
// ... left as an exercise for the reader ...
}
I do exactly this sort of thing in my book, in http://www.apeth.com/iOSBook/ch21.html#_editable_content_in_table_items (that's where the above code comes from), so take a look and see what ideas it gives you.
When the user is done entering text you could do something like the following which maps the index paths of the rows in your tableview to the indices in an array.
- (NSMutableArray *)updateText {
NSUInteger cellsCount = [self.tableView numberOfRowsInSection:0];
NSMutableArray *cellTextArray = [[NSMutableArray alloc] initWithCapacity:cellsCount];
for(NSInteger i = 0; i < cellsCount; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
CustomCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSString *item = cell.itemNumber.text;
[cellTextArray insertObject:item atIndex:i];
}
return cellTextArray;
}
Assuming your cell has the UITextFieldDelegate set, when the user is done entering text you can do something like this:
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self.delegate didFinishEditing];
}
Where self.delegate is the UITableViewController, which in turn call updateText when necessary.
Things to be careful of - the for loop in updateText needs to loop over the tableview and dequeue cells for each index path. Simply using the tableview's visible cells would most likely leaving you missing text from cells that were off screen and got reused.
Hope this helps and good luck!
There are obviously a few aspects of this problem. First of all, you want to be able to recover references to the UILabel's, so that you can figure out which row a specific UILabel is in. I'd recommend doing this using the tag property, like this:
_item = [[UILabel alloc] init];
_item.tag = 100; // or any value
[self.contentView addSubview:_item];
You also need to set an action that gets called whenever the text in the label gets changed. You can do that like this:
[_item addTarget:someObject
action:#selector(labelChanged:)
forControlEvents:UIControlEventEditingChanged];
Whatever class someObject is, it needs to have a method with this signature:
- (void)labelChanged:(id)sender;
Inside that method you can check that sender is in fact a UILabel, and then you can access the new text with sender.text.
In order to figure out what point in the array to put the text in, you can declare a loop over the number of rows in your table:
for (int i = 0; i < [mainViewControllerInstance tableView:theTableInQuestion numberOfRowsInSection:0]; ++i) {
if(sender == [[mainViewControllerInstance tableView:theTableInQuestion
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] viewWithTag:100]) {
// Put `sender.text` in the appropriate spot in your array
}
}
A few additional notes: I'd use an NSMutableArray to keep track of your strings, since you'll be updating them, but I'm not entirely sure what best practices are here. You'll also want to make sure you initialize your array (whether you make it an NSArray or an NSMutableArray) to have the proper number of rows, and make sure that you add rows to it when the + button is pressed, or you'll risk getting an exception when you try to change an entry for a row that doesn't yet exist.
You might also want to have a look at the free Sensible TableView framework. The framework performs almost all what you need automatically. Should probably save you a lot of manual work. Good luck!

TableView is not loading data?

In my Map application I have segment controller on main screen and using that I have Map View & tableview(both are UIView). I have tried everything but my data is not loading in my tableview. Here is my tableview code. Here marker is attribute in my xml file which contain Showroom name and Iam able to parse this.
.h file
#interface HViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {
UITableView *_tableView;
}
#property (nonatomic, retain) IBOutlet UITableView *_tableView;
#end
.m file
Edited = with ViewWillAppear,viewDieLoad, segement action method
#synthesize tableView;
- (void)viewDidLoad {
appDelegate = (HAppDelegate *)[[UIApplication sharedApplication] delegate];
[segmentedControl addTarget:self action:#selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated
{
self._tableView.rowHeight = 80.0;
[_tableView reloadData];
}
-(void)segmentAction:(id)sender;{
UISegmentedControl* segCtl = sender ;
if( [segCtl selectedSegmentIndex] == 0 )
{
NSLog(#"segmentAction mapView");
mapView.hidden = NO;
_tableView.hidden = YES;
//[ self.view addSubview:mapView] ; // adding view to segmentindex 0
}
if( [segCtl selectedSegmentIndex] == 1 )
{
NSLog(#"segmentAction _tableview");
_tableView.hidden = NO;
mapView.hidden = YES;
//[ self.view addSubview:_tableview] ;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"appDelegate.markers _tableview");
return [appDelegate.markers count];
}
//method to print row(showroom count on Header)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section {
if(section == 0)
return [NSString stringWithFormat:NSLocalizedString(#"ShowRooms[%d]", #"Showroom format"), [appDelegate.markers count]];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSUInteger const kShowroomNameLabelTag = 2;
UILabel *ShowroomNameLabel = nil;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
ShowroomNameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(50, 1, 300, 20)] autorelease];
ShowroomNameLabel.tag = kShowroomNameLabelTag;
ShowroomNameLabel.font = [UIFont boldSystemFontOfSize:18];
[cell.contentView addSubview:ShowroomNameLabel];
NSLog(#"UITableViewCell.markers _tableview");
}
else
{
ShowroomNameLabel = (UILabel *)[cell.contentView viewWithTag:kShowroomNameLabelTag];
}
marker *aMarker = [appDelegate.markers objectAtIndex:indexPath.row];
//ShowroomNameLabel.text = aMarker.name;
ShowroomNameLabel.text= [NSString stringWithFormat:#"ShowroomName= %#", aMarker.name];
return cell;
}
In my tableview Header it shows data count correctly but not showing data.
I have connected delegate,datasource,_tableview to fileOwner of the HViewController in which I have put above code. Plz suggest something where I am wrong. I am parsing XML file and getting data in console alos I can show it on Map. But I am not able to load data in my tableview.
Try moving [_tableView reloadData] to viewWillAppear.
UITableViewController reloads the table view's data in viewWillAppear, not viewDidLoad. I can't tell you the exact reason for which this would make a difference, though I can think of several. Anyway, it's worth a try.
EDIT:
RESPONSE TO COMMENTS
If titleForHeaderInSection: is being called, then there is a data source connected to a table view. So, the problem is not a lack of a data source connection.
I am guessing you have 2 table views in your .xib file: a large one & a short one below it. The large table view is not connected to the data source, so it just displays blank lines. The short table view is connected to the data source. But, it is just tall enough for a header and has no space left to display any cells. Thus, titleForHeaderInSection: is called, but cellForRowAtIndexPath: is not because there is no space to display any cells.
Note that this is just a guess, but it is the only scenario I can think of that would cause the behavior you described. The code you posted looks ok, although a bit more complicated than necessary.
There is no question that reloadData should be in viewWillAppear. That's where the Apple engineers put it when they created the UITableViewController class. So, to put it elsewhere, you have to believe you know better than they do.

how to trigger update of a custom UITableViewCell after returning to view via back button?

Background
have a custom UITableCellView in which I'm using a custom UITextField that I've added as a subview (i.e. not using normal UITableCellView views
in the scenario is pressing on the cell => jump to screen to modify value (via pushViewController / navigationControl). Then after changing hitting the BACK button to go back to the UITableView
using this approach there is no specific call back for that scenario, so I've been using the approach where you trap this using the general viewDidAppear method of the UITableViewController - the technique I'd used to update the change was:
Code:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.myNormalCell.textLabel.textColor = myColor;
[self.tableView reloadData];
}
But what I note is that the above code:
works for for normal/existing fields in a UITableViewCell
does NOT work for my custom textField subviews I've put in my custom UITableViewCell
QUESTION - How to, in this use case, get my custom fields to be udpated/shown on the UITableView when I come back to it after making a change?
For example:
do I need to somehow set my custom field/subview as "needs to be updated"?
do I need to override reloadData somewhere/somehow to set the custom field?
EDIT: Add some code:
CODE FROM cellForRowAtIndexPath
(a) Code that works with standard UITableViewCell
self.lookAheadDaysCell = (ConfigCell*)[tableView dequeueReusableCellWithIdentifier:#"LookAheadWeeks"];
if (self.lookAheadDaysCell == nil) {
self.lookAheadDaysCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"LookAheadWeeks"] autorelease];
self.lookAheadDaysCell.textLabel.text = #" Weeks into Future";
self.lookAheadDaysCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
self.lookAheadDaysCell.detailTextLabel.text = [self.weConfig.lookAheadWeeks stringValue];
return self.lookAheadDaysCell;
(b) Code that doesn't work with custom field in custom cell
self.lookAheadDaysCell = [ConfigCell cellForReuseId:#"LookAheadWeeks"
TableView:tableView
Title:#"Number of Weeks"
ShowHelp:true
Tag:9999
Delegate:self
UseTextField:false
ContentText:[self.weConfig.lookAheadWeeks stringValue]];
return self.lookAheadDaysCell;
CODE FROM CUSTOM CELL
Interface:
#interface ConfigCell : UITableViewCell <UITextFieldDelegate> {
UITextField *_textField;
UILabel *_titleLabel;
}
#property (nonatomic, retain) UITextField *textField;
#property (nonatomic, retain) UILabel *titleLabel;
+ (ConfigCell*) cellForReuseId:(NSString *)reuseId TableView:(UITableView*)tableView Title:(NSString*)titleStr ShowHelp:(BOOL)showHelp Tag:(NSInteger)tag Delegate:(id)delegate UseTextField:(BOOL)useTextField ContentText:(NSString*)text;
#end
Key Methods:
+ (ConfigCell*) cellForReuseId:(NSString *)reuseId TableView:(UITableView*)tableView Title:(NSString*)titleStr ShowHelp:(BOOL)showHelp Tag:(NSInteger)tag Delegate:(id)delegate UseTextField:(BOOL)useTextField ContentText:(NSString*)text
{
// Get Cell (via reuse or create a new one)
ConfigCell *cell = (ConfigCell*)[tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
// Create Cell
cell = [[[ConfigCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// View 1 - Title Label
<<cut>>
// View 2 - TextField for entry
cell.textField = [[[UITextField alloc] initWithFrame:CGRectMake(0,0,0,0)] autorelease];
cell.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
cell.textField.font = [UIFont systemFontOfSize:16];
cell.textField.textAlignment = UITextAlignmentLeft;
cell.textField.text = text;
if (useTextField) {
cell.textField.delegate = delegate;
cell.textField.tag = tag;
cell.textField.returnKeyType = UIReturnKeyDone;
} else {
cell.textField.userInteractionEnabled = false;
}
[cell.contentView addSubview:cell.textField];
}
return cell;
}
well I did fix it by putting the following line in "viewDidAppear"
self.lookAheadDaysCell.textField.text = [self.weConfig.lookAheadWeeks stringValue];
however it doesn't really help me understand why I didn't need this line when I was using the standard text fields in a UITableViewCell before...noting I'm effectively setting the value of the cell text by reference

Grouped style setting not taking effect from IB, UITableView

I've created a table view in an iPhone app using Interface Builder (IB). My table style is set to Grouped, and displays that way in IB. However, whenever I build and run, it shows up as a Plain style in the simulator.
I have another view set to Grouped and don't experience this problem. Has anyone run into this problem before?
The rest of the view is not created programmatically, so I'd like to avoid doing that for this view. There must be something I'm missing.
The only tableView method I'm doing much of anything in is the cell handler method where I'm incorporating a text box into select fields:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = #"Title";
UITextField *listTitleTextField = [ [ UITextField alloc ] initWithFrame: CGRectMake(150, 10, 145, 38) ];
listTitleTextField.adjustsFontSizeToFitWidth = YES;
listTitleTextField.textColor = [UIColor blackColor];
listTitleTextField.font = [UIFont systemFontOfSize:17.0];
listTitleTextField.placeholder = #"Your Title";
listTitleTextField.backgroundColor = [UIColor whiteColor];
listTitleTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
listTitleTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
listTitleTextField.textAlignment = UITextAlignmentRight;
listTitleTextField.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
listTitleTextField.returnKeyType = UIReturnKeyDone;
listTitleTextField.tag = 0;
listTitleTextField.delegate = self;
listTitleTextField.clearButtonMode = UITextFieldViewModeUnlessEditing; // no clear 'x' button to the right
if (self.wishList.listTitle == nil || [self.wishList.listTitle length] == 0) {
listTitleTextField.text = #"";
}
else {
listTitleTextField.text = self.wishList.listTitle;
}
[listTitleTextField setEnabled: YES ];
[cell addSubview: listTitleTextField ];
[listTitleTextField release];
}
else if (indexPath.row == 1) {
cell.detailTextLabel.text = #"Pick an Icon";
cell.textLabel.text = #"List Icon";
}
}
else {
cell.textLabel.text = #"Add Wish";
}
return cell;
}
We had this and it stumped us for about an hour. Finally we found that we had failed to set the NIB Name property of the view controller inside the navigation controller (inside the tab bar controller, in our main XIB!). Without that set, all the changes we made to the XIB file of our table view were completely ignored. Yet the code worked fine otherwise, and there was very little to point us to our mistake.
I bet something similar is going on in your case. Make sure your XIB (where you set the table style) is actually being used.
What is your controller's base class? I had the same problem, until I switched my controller from subclassing a UITableViewController to just a standard UIViewController. It would seem if you're using a UITableViewController, it has its own built-in UITableView and ignores the one you specify in IB.
Still not sure why the "grouped" style setting is not taking effect from the Interface Builder. However, you can manually set it before the view is created here:
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
style = UITableViewStyleGrouped;
if (self = [super initWithStyle:style]) {
}
return self;
}
When initialising the view just do the following:
CustomTableViewController *viewController = [[CustomTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
Rather than just init. This solves it for me.
You need to specify the Nib name when you create the class or else it will not look for the default nib named the same as your class. For some reason Apple decided to include its own table view as a default.
For example if your class name is "Settings":
settingsController = [[Settings alloc] initWithNibName:#"Settings" bundle: nil];
Does the table view respond to events and get populated with data as you'd expect? - it sounds like the outlets are not connected properly to me. I'd double check the datasource and delegate connections from the tableview to the controller. May be worth deleting the tableview from IB and re-adding it and re-connecting it as well. I've seen IB act a little finicky in the odd case.