tags:

views:

203

answers:

2

hi my tableview show like this when i scroll .......will u help what mistake i have done? any help please?if you see that image , the value i have selected in over writing........

http://www.freeimagehosting.net/image.php?fa76ce6c3b.jpg

the code is

  • (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];
 cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}




CGRect rectLbl2 = CGRectMake(75 ,1, 215, 32);
CGRect rectLbl3 = CGRectMake(75 ,28, 215, 30);
addLabel2= [[UILabel alloc] initWithFrame:rectLbl2];
addLabel3= [[UILabel alloc] initWithFrame:rectLbl3];



addLabel2.text = @"senthil";
[cell addSubview:addLabel2];
[addLabel2 release]; // for memory release


addLabel3.text= @"senthil";
[cell addSubview:addLabel3];
[addLabel3 release];


return cell;

}

A: 

The problem in my opinion is that you are re-allocating a new label each time the tableview scrolls and that is causing it to be re-wrote to the cell.

Also make sure you are using dequeueReusableCellWithIdentifier properly. It may be the case that you are not dequeuing it properly.

Thats all i can say without looking at your code.

Chintan Patel
i have given my code
Mikhail Naimy
The problem is that you are allocating the labels again every time it scrolls. You can allocate the label in viewDidLoad and then just set the text in cellForRowAtIndexPath.
Chintan Patel
As a rule, always make sure there are no "alloc" calls in cellForRowAtIndexPath. Otherwise it will be called every time you scroll the table view and will result in overwriting the same label again and again.
Chintan Patel
+1  A: 

Dequeueed cells already have a label so you are adding another label to these cells. Add the label in the initial UITableViewCell creation and just change the label contents.

Try something like this:

(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];
            cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

     UILabel *lab;
     CGRect rectLbl2 = CGRectMake(75 ,1, 215, 32);
     lab = [[UILabel alloc] initWithFrame:rectLbl2];
     lab.tag = 2;
     [cell addSubview:lab];
     [lab release];

     CGRect rectLbl3 = CGRectMake(75 ,28, 215, 30);
     lab = [[UILabel alloc] initWithFrame:rectLbl3];
     lab.tag = 3;
     [cell addSubview:lab];
     [lab release];
    }

    ((UILabel *)[cell viewWithTag:2]).text = @"senthil";
    ((UILabel *)[cell viewWithTag:3]).text = @"senthil";

    return cell;
}
zaph
thank you very much....
Mikhail Naimy