0

I created an application using UITableView. I parsing the data from URL and listed in UITableView.

But in UITableView, the first the data only displayed again and again.

I don't know why this problem occurred.

My code is as below:

-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    CGRect frame = CGRectMake(10.0f, 5.0, 300.0, 55);
    valueField = [[UILabel alloc] initWithFrame:frame];
    [valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    valueField.tag = 111;
    valueField.font=[UIFont fontWithName:@"Helvetica" size:16.0];
    valueField.textAlignment = UITextAlignmentLeft;
    valueField.lineBreakMode=UILineBreakModeWordWrap;
    valueField.numberOfLines = 0;
    valueField.textColor = [UIColor whiteColor];
    valueField.highlightedTextColor = [UIColor whiteColor];
    valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    valueField.adjustsFontSizeToFitWidth = YES;
    valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    valueField.text=[title1 objectAtIndex:count1];
    [cell.contentView addSubview:valueField];
    UIImage *favOn = [UIImage imageNamed:@""];
    UIImage *favOff = [UIImage imageNamed:@""];                   
    titlebutton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 15.0, 300.0, 55)];
    [titlebutton setTag:indexPath.row];
    //titlebutton.tag = 111;
    [titlebutton setImage:favOff forState:UIControlStateNormal];
    [titlebutton setImage:favOn forState:UIControlStateSelected];
    if([self getFavState:@"111"]) 
    {
        [titlebutton setSelected:YES];
    }
    else 
    {
        [titlebutton setSelected:NO];
    }
    [titlebutton addTarget:self action:@selector(favButtonSwitch:) forControlEvents:UIControlEventTouchUpInside];
    [titlebutton setBackgroundColor:[UIColor clearColor]];
    [titlebutton setTitle:@"" forState:UIControlStateNormal];
    [cell.contentView addSubview:titlebutton];
    CGSize labelSize = [valueField.text sizeWithFont:valueField.font constrainedToSize:valueField.frame.size lineBreakMode:UILineBreakModeWordWrap];
    int labelHeight = labelSize.height;
    NSLog(@"labelHeight = %d", labelHeight);

    frame = CGRectMake(10.0f, 40.0, 300.0, 55);
    pubField = [[UILabel alloc] initWithFrame:frame];
    [pubField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    pubField.tag = 111;
    pubField.font=[UIFont fontWithName:@"Helvetica" size:12.0];
    pubField.textAlignment = UITextAlignmentLeft;
    pubField.lineBreakMode=UILineBreakModeWordWrap;
    pubField.numberOfLines = 0;
    pubField.textColor = [UIColor whiteColor];
    pubField.highlightedTextColor = [UIColor whiteColor];
    pubField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    pubField.adjustsFontSizeToFitWidth = YES;
    pubField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    pubField.text=[pubDate objectAtIndex:indexPath.row];
    [cell.contentView addSubview:pubField];

    frame = CGRectMake(5.0, 70.0, 300.0, 55);
    desField = [[UILabel alloc] initWithFrame:frame];
    [desField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    desField.tag = 111;
    desField.font=[UIFont fontWithName:@"Helvetica" size:13.0];
    desField.textAlignment = UITextAlignmentLeft;
    desField.lineBreakMode=UILineBreakModeWordWrap;
    desField.numberOfLines = 0;
    desField.textColor = [UIColor whiteColor];
    desField.highlightedTextColor = [UIColor whiteColor];
    desField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    desField.adjustsFontSizeToFitWidth = YES;
    desField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    desField.text=[description objectAtIndex:indexPath.row];
    [cell.contentView addSubview:desField];
    }

    UIImage *patternImage = [UIImage imageNamed:@"bg.jpg"];
    UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    backView.backgroundColor = [UIColor colorWithPatternImage: patternImage];
    cell.backgroundView = backView;

    NSString *image1 =[images objectAtIndex:indexPath.row];
    NSLog(@"Images  .....   = %@",image1);
    NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:indexPath.row]]];
    UIImage *myimage = [[UIImage alloc] initWithData:mydata];
    UIImageView *imageView = [ [ UIImageView alloc ] initWithImage: myimage ];
    imageView.frame = CGRectMake(10.0f, 110.0f, 120.0f, 120.0f); 
    [cell.contentView addSubview:imageView];
    return cell;
    }

4 Answers 4

2

do all the alloc/init and all things, that will be the same in any cell in the

if(cell==nil){
    //<-- here
}

while setting text and any thing that changes by the specific data outside the block

if(cell==nil){

}
//<-- here

i.e:

if(cell==nil){
    //....
    valueField.tag = 111;
    valueField.font = [UIFont fontWithName:@ "Helvetica" size:16.0];
    valueField.textAlignment = UITextAlignmentLeft;
    valueField.lineBreakMode = UILineBreakModeWordWrap;
    valueField.numberOfLines = 0;
    valueField.textColor = [UIColor whiteColor];
    valueField.highlightedTextColor = [UIColor whiteColor];
    valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    valueField.adjustsFontSizeToFitWidth = YES;
    valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    [cell.contentView addSubview : valueField];
    //....
}
//....
valueField.text = [title1 objectAtIndex:count1];
//....
0
1

the first cell data is getting displayed in all the cells because when you initialize and create your reusable cell, you fill the data there, you need to initialize a reusable cell that doesnt have all its values set, and then set this information for each cell. That is, you need to have all the information you want for each particular cell outside the if(cell==nil) part of this code, and rely on the indexPath to know which cell you are in, and feed in different data based on this.

1

You seem to be constructing the subviews of the cell correctly when there is no reusable cell available. The question is, what does your code do when you do dequeue a reusable cell? It appears that you do nothing. You should move any row-specific initialization (setting label text values, images, etc.) outside the check for whether the dequeued cell is nil. In this way, whether you just constructed a new cell or dequeued a previously-created cell, the data being displayed correctly correlates to the row being displayed.

0

Well actually your code this way is quite hard to decode,
but first of all, you declare a lot of tag that should be used to retrieve the UIViews you're declaring. Unfortunately these are all the same (tags) so you might never get what you want. You should maybe put in some constant in your header file like:

#define kPubLabel             100

then use them.

then you usually retrieve these after declaring your cell to populate them.

In the mean time, retrieving your images from url in this method is one of the best way to have your application being slow, since each time you scroll, you'll have to re-fetch your image from where it come from causing your table view to "freeze".

Finally, the only thing that might change, according to your code is the imageView that you add to your frame. You usually have an ImageView in which you put an image that you might have stored/cached but rarely change the contentView of a cell.

ie
Maybe you should externalize your cell building code in a class overriding UITableViewCell. Put different tags on every element in your cell to retrieve them in your method.
Retrieve your elements in your method according to there tags.
Populate and only populate them in your method.
Retrieve your image once and for all for performance improval.

I'm not sure this will help, but in my point of view, there are actually a lot work to do in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.