PDA

View Full Version : need help populating table cells with a variable number of text fields




manman
Aug 31, 2008, 01:13 AM
I need help doing something similar to what the "UICatalog" sample app code does in the "text field" section

I am creating a table that uses a custom cell type (it's called 'CellTextField', same class as in the sample app) to wrap text field controls. The table will have a variable number of these cells depending on other parameters of the app.

My table is loading correctly, and the cells all appear to be loading correctly as well (they all have the correct title, the cells are all showing up etc). My problem is that the text field is only one text field is showing up (in the first cell). If I scroll down, that text field seems to jump around to the very bottom cell just appearing (i'm guessing this has something to do with the view redrawing itself?). Also, it looks like it's the same text field, because if I type something in it, when it jumps around it still has the same text...

So I'm wondering if there is something wrong with the way i'm allocating the text fields and passing them to the custom cell. Bellow is some code where the error might be..

This code is in the view controller where I set up the table

-(UITextField*) createTextField
{
CGRect frame = CGRectMake( 0.0, 0.0, 100.0, 30.0);
UITextField *returnTextField = [[UITextField alloc] initWithFrame: frame];

returnTextField.borderStyle = UITextBorderStyleRoundedRect;
returnTextField.textColor = [UIColor blackColor];
returnTextField.backgroundColor = [UIColor whiteColor];
returnTextField.autocorrectionType = UITextAutocorrectionTypeNo;
returnTextField.keyboardType = UIKeyboardTypeDefault;
returnTextField.returnKeyType = UIReturnKeyDone;
returnTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

return returnTextField;
}

-(void)loadView
{
UIView *contentView= [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
contentView.autoresizesSubviews = YES;
self.view = contentView;
[contentView release];

NSInteger i;

textFieldList = [[NSMutableArray alloc] init];
for(i=0; i<wordList.count ; i++)
{
UITextField* tempTF = [[UITextField alloc] init];
tempTF = [self createTextField];
[textFieldList addObject:tempTF];
[tempTF release];
}

//create tableView
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
myTableView.delegate = self;
myTableView.dataSource = self;

[self.view addSubview:myTableView];

}

And here are some delegate methods where I set up the table cells. these are taken directly from the sample code pretty much, although as you can see I commented some things out for testing


-(UITableViewCell *)obtainTableCellForRow:(NSInteger)row
{
UITableViewCell *cell = nil;

//cell = [myTableView dequeueReusableCellWithIdentifier:kCellTextField_ID];

if(cell == nil)
{
cell = [[CellTextField alloc] init];// WithFrame:CGRectZero reuseIdentifier:kCellTextField_ID]autorelease];
((CellTextField*)cell).delegate = self;
}

return cell;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
UITableViewCell *sourceCell = [self obtainTableCellForRow:row];

((CellTextField*) sourceCell).view = [textFieldList objectAtIndex:row];


return sourceCell;
}



The 'textFieldList' looks like it does have the right amount of text field objects in it, and the 'values' in the debugger look different for each one so i'm not sure where I'm going wrong here.

Any help would be greatly appreciated. Thanks!



manman
Aug 31, 2008, 01:54 AM
oops, stupid mistake... problem solved.

I was pulling from the array based on the row (always 0) instead of the section... :(