views:

76

answers:

1

i am creating a UITableview cell in the following way

const NSInteger TOP_LABEL_TAG = 1001;

UILabel *topLabel;

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{

    cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero             reuseIdentifier:CellIdentifier]
     autorelease];

    UIImage *indicatorImage = [UIImage imageNamed:@"indicator.png"];

     cell.accessoryView =[[[UIImageView alloc] initWithImage:indicatorImage]
     autorelease];

   const CGFloat LABEL_HEIGHT = 25;

    UIImage *image = [UIImage imageNamed:@"64x64.png"];

      topLabel =[[[UILabel alloc] initWithFrame:CGRectMake(
                 image.size.width + 2.0 * cell.indentationWidth,
                 0.8 * (tableView.rowHeight - 1.7 * LABEL_HEIGHT),
                 tableView.bounds.size.width -
                 image.size.width - 4.0 * cell.indentationWidth
                 - indicatorImage.size.width,
                 LABEL_HEIGHT)] autorelease];

    [cell.contentView addSubview:topLabel];


        topLabel.tag = TOP_LABEL_TAG;

    topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
    topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];

    topLabel.font = [UIFont systemFontOfSize:20];
    topLabel.textAlignment== UITextAlignmentCenter;

}

else
{

    topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
}   

topLabel.text =  [NSString stringWithFormat:[aboutArray objectAtIndex:[indexPath row]]];
topLabel.textAlignment=UITextAlignmentCenter;

first time when the table loads the tableView works fine.But when i navigate back to this page from another page the text in the first cell of table moves on the right and only half of it visisble..What could be the possible reason for it??

A: 

There are a few bugs here working together to give you troubles.

For starters, I think you might be assuming that the dequeued reusable cell has all the same data in it that it had when you get it back. The way it actually works is you may get back the same cell, but you will probably get a different cell, which is left over from the top or bottom depending on the scroll direction. As cells go out of view, they are flagged as reusable in no predictable order. So, remove that else block, and move all the logic (other than the init + autorelease outside of the if block. This way, all you are really checking is if you need to allocate new memory.

Secondly, you have == UITextAlignmentCenter, you probably want to use =

Thirdly, the math you are using to calculate your label geometry is suspect. It seems you are loading an image, then using the size of that image and the table bounds to dynamically calculate a new label frame. I would seriously consider a new way of approaching this label frame calculation. Are you sure this is the only way you can do it? It seems to me your label frame math ought to be the same every time.

I've cleaned it up a bit so it's readable and put it below with comments to better illustrate what I'm trying to say. Hope this helps.

// initialize cell if need be
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
  cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// setup the accessory
UIImage *indicatorImage = [UIImage imageNamed:@"indicator.png"];
cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];


// TODO: rewrite this 
UIImage *image = [UIImage imageNamed:@"64x64.png"];
const CGFloat LABEL_HEIGHT = 25;
CGFloat x = image.size.width + 2.0 * cell.indentationWidth;
CGFloat y = 0.8 * (tableView.rowHeight - 1.7 * LABEL_HEIGHT);
CGFloat w = tableView.bounds.size.width - image.size.width - 4.0 * cell.indentationWidth - indicatorImage.size.width;
CGFloat h = LABEL_HEIGHT;
CGRect labelFrame = CGRectMake(x,y,w,h);

// build a label and use it as the content view
UILabel* topLabel = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:20];
topLabel.textAlignment = UITextAlignmentCenter;
topLabel.text = [NSString stringWithFormat:[aboutArray objectAtIndex:[indexPath row]]];
topLabel.textAlignment = UITextAlignmentCenter;

[cell.contentView addSubview:topLabel];
slf
thnku i wuld try this method of label calculation.
Hemant
it would be better to do this as a custom table view cell from IB
slf