1

i'm working on a tableview and i am newbie in dynamically creating cells (i used to create them with IB and then link them to their tableviewcell controller etc..).

Everything works great and recpected arrays are updated properly but when i fire [self.tableview reloadData] the program just redraws new values over old cells. for example if there "TEST CELL" value in a uilabel inside a cell, when i update the data to "CELL TEST" and fire the reloadData, the uilabel looks like there are two labels on top of each other and both values are visible. (its like creating two uilabel with exact same location and same size and setting their values)

and this event happens everytime i fire reloadData, with each reload, the program looks like its adding another uilabel on top of the older one. heres my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *lblText1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 5, 130, 30)];
lblText1.adjustsFontSizeToFitWidth = YES;
lblText1.backgroundColor = [UIColor clearColor];
lblText1.text = [lblText1Array objectAtIndex:indexPath.row];
[cell addSubview:lblText1];
[lblText1 release];
if(indexPath.row<3)
{   
    UILabel *lblText2 = [[UILabel alloc] initWithFrame:CGRectMake(170, 5, 130, 30)];
    lblText2.adjustsFontSizeToFitWidth = YES;
    lblText2.backgroundColor = [UIColor clearColor];
    lblText2.text = nil;
    lblText2.text = [spParameters objectAtIndex:indexPath.row];
    [cell addSubview:lblText2];
    [lblText2 release];
}
else if(indexPath.row==3){
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(170, 7, 130, 30)];
    textField.adjustsFontSizeToFitWidth = YES;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"please insert value";
    textField.textAlignment = UITextAlignmentCenter;
    textField.delegate = self;
    textField.text = [spParameters objectAtIndex:indexPath.row];
    [cell addSubview:textField];
    [textField release];
}
else if(indexPath.row == 4)
{
    UISwitch *gSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(170, 7, 130, 30)];
    [gSwitch setOn:FALSE];
    [gSwitch addTarget: self action: @selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    [cell addSubview:gSwitch];
    [gSwitch release];
}
// Configure the cell...
return cell;

}

im releasing the components after i adding them to subviews, and i am thinking about if theres something wrong with the reuseidentifier...

Thanx for helping.

dreampowder
  • 1,644
  • 3
  • 25
  • 41

1 Answers1

1

It looks as though you're populating a detail view with a fixed number of cells, so you should consider creating the instances statically, for example in viewDidLoad or in Interface Builder. You could store each cell in a separate instance variable, and just return the one that corresponds the current row each time tableView:cellForRowAtIndexPath: is called.

If you create the cells programmatically, add whatever subviews you need at that time. Otherwise, as I mentioned, you could do that in Interface Builder, which often makes it easier to set up the details of controls such as text fields. Note though that UITableViewCell already contains an instance of UILabel, so adding one yourself is redundant. Instead, just access the cell's textLabel property to get its label.

jlehr
  • 15,557
  • 5
  • 43
  • 45
  • Also, do yourself a favor and at least skim the Table View Programming Guide for iOS. This stuff is difficult to do correctly without reading the docs. – jlehr Feb 08 '11 at 16:44
  • Thanks for the answer, i usually do like you said, but i started to experiment with addsubview so i totally forgot using default cell labels. now i've changed cell style to UITableViewCellStyleValue1 and using textlabel and detailtextlabel for setting properties. – dreampowder Feb 08 '11 at 16:56
  • looks like i need to read guide documents more carefully. thanx for the tip. – dreampowder Feb 08 '11 at 16:57
  • That's not sufficient to solve the problem though -- you still need to modify your code so that it's not creating the cells and adding their subviews dynamically. (You probably knew that, but just to be sure...) :-) – jlehr Feb 08 '11 at 16:58
  • i've just removed all the addsubviews except the uiswitch.. What about giving different reuse identifiers for each different cell? would it solve the problem? – dreampowder Feb 08 '11 at 20:58
  • 1
    Why do you need reuse identifiers at all? You're caching the cells yourself, so you don't need to use the table view's dequeueing mechanism. – jlehr Feb 08 '11 at 21:47