I found some example code of how to implement a textfield into a cell here: Having a UITextField in a UITableViewCell
However the textfield appears multiple times on my table in the 1st section of the table, even though i specified it to only appear when it comes up to the 2nd section of the table and the first row of that section.
Any explanations why this happens? I only want it in the 2nd section 1 st row. but it appears to think that whenever the 2nd section is coming up it will just draw the textfield in advance. Is there a way to "lock" it to a particular group and cell?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self CreateMultilinesCell:CellIdentifier];
}
NSLog(@"%d", [indexPath section]);
if ([indexPath section] == 1) {
NSLog(@"TextField");
if ([indexPath row] == 0 ) {
UITextField *replyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 185, 30)];
replyTextField.adjustsFontSizeToFitWidth = YES;
replyTextField.textColor = [UIColor blackColor];
replyTextField.keyboardType = UIKeyboardTypeDefault;
replyTextField.returnKeyType = UIReturnKeyDone;
replyTextField.backgroundColor = [UIColor grayColor];
replyTextField.autocorrectionType = UITextAutocorrectionTypeNo;
replyTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
replyTextField.textAlignment = UITextAlignmentLeft;
replyTextField.tag = 0;
replyTextField.delegate = self;
replyTextField.clearButtonMode = UITextFieldViewModeNever;
[replyTextField setEnabled: YES];
[cell.contentView addSubview:replyTextField];
[replyTextField release];
cell.detailTextLabel.text = @"something";
}
}
else {
cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
}
return cell;
}
createmultilinecell:
- (UITableViewCell*) CreateMultilinesCell :(NSString*)cellIdentifier
{
//NSLog(@"Entering CreateMultilinesCell");
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.font = [self SubFont];
cell.detailTextLabel.textColor = [UIColor colorWithRed:10.0/255 green:10.0/255 blue:33.0/255 alpha:1.0];
[cell setBackgroundColor:[UIColor clearColor]];//]colorWithRed:.98 green:.98 blue:.99 alpha:1.0]];
[self.tableView setBackgroundColor:[UIColor clearColor]];//colorWithRed:.94 green:.96 blue:.99 alpha:1.0]];
//NSLog(@"Exiting CreateMultilinesCell");
return cell;
}
argh i'm too low to answer my own question so i'll just update here:
Looks like making 2 cellidentifiers work. Thanks. Just learnt something new! haha.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSString *replyCellIdentifier = @"replyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *replyTextField;
if (cell == nil) {
if ([indexPath section] == 0) {
cell = [self CreateMultilinesCell:CellIdentifier];
}
else if ([indexPath section] == 1) {
NSLog(@"TextField");
cell = [self CreateMultilinesCell:replyCellIdentifier];
if ([indexPath row] == 0) {
replyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 185, 30)];
replyTextField.adjustsFontSizeToFitWidth = YES;
replyTextField.textColor = [UIColor blackColor];
replyTextField.keyboardType = UIKeyboardTypeDefault;
replyTextField.returnKeyType = UIReturnKeyDone;
replyTextField.backgroundColor = [UIColor grayColor];
replyTextField.autocorrectionType = UITextAutocorrectionTypeNo;
replyTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
replyTextField.textAlignment = UITextAlignmentLeft;
replyTextField.tag = 0;
replyTextField.delegate = self;
replyTextField.clearButtonMode = UITextFieldViewModeNever;
[replyTextField setEnabled: YES];
[cell.contentView addSubview:replyTextField];
[replyTextField release];
cell.detailTextLabel.text = @"something";
}
}
/*else {
cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
}*/
}
NSLog(@"%d", [indexPath section]);
if ([indexPath section] == 0) {
cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
}
return cell;
}
thanks to everyone that contributed.
In terms of functionality i'm not so sure yet but it's one step closer to getting where i want it to be :).