Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>nessence has a lot of good information. A few more thoughts:</p> <p>You're leaking like crazy here. Every cell you create, you leak 2 UILabels, a UIImageView and a UIImage.</p> <p>As noted before, not only are you leaking these, they're accumulating in your view because you're sticking one on top of the other with addSubview:.</p> <p>Reaching out to the network during a cell draw is incredibly slow and is blocking your UI. If these URLs are local, then you can use UIImage's +imageWithContentsOfFile, if not, you need to load this in the background.</p> <p>I don't think you need a thread here. NSURLConnection is an excellent way to load data in the background without incurring the overhead of threading.</p> <p>nessence is completely correct that you need a model class for Story.</p> <p>Your basic approach to reusable cell configuration is incorrect. You don't fetch a reusable cell and then add subviews to it. All your subviews should be added in the if() block to create the cell. Then, in each pass, you just change the values of things. I've rewritten some of your code below to demonstrate. This is still not correct code, because it is reaching out to the network during a cell draw, and this may be too many elements to be in a subview-cell (rather than a custom cell), but it's closer to the right idea. I don't even know if this compiles; I just typed it here.</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { // Here we do all our cell creation cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; // Make the label CGRect aframe = CGRectMake(80, 30, 250, 40); UILabel *textLabel = [[[UILabel alloc] initWithFrame:aframe] autorelease]; // Note the -autorelease so we don't leak textLabel.font = [UIFont boldSystemFontOfSize:14]; textLabel.numberOfLines=0; textLabel.textColor = [UIColor darkTextColor]; textLabel.backgroundColor = [UIColor whiteColor]; textLabel.tag = DescriptionTag; // A tag so we can find it later (you'll need a constant for this) [cell.contentView addSubview:textLabel]; // And the second label aframe = CGRectMake(80, 30, 250, 40); textLabel = [[[UILabel alloc] initWithFrame:aframe] autorelease]; textLabel.font = [UIFont boldSystemFontOfSize:14]; textLabel.numberOfLines=0; textLabel.textColor = [UIColor darkTextColor]; textLabel.backgroundColor = [UIColor whiteColor]; textLabel.tag = TitleTag; [cell.contentView addSubview:textLabel]; // The image view CGRect frame = CGRectMake(0, 0, 70,80); UIImageView *topImageView = [[[UIImageView alloc] init] autorelease]; topImageView.frame = frame; topImageView.tag = TopImageTag; [cell.contentView addSubview:topImageView]; } // all the above was cell creation; we do that as seldom as possible. // Now we do cell configuration. We want this to be fast. UILabel *descriptionLabel = (UILabel*)[cell.contentView viewWithTag:DescriptionTag]; descriptionLabel.text = itemDescription; UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:TitleTag]; titleLabel.text =[[stories objectAtIndex:indexPath.row] objectForKey:@"title"]; NSString *imageURLString = [m_imglinkArray objectAtIndex:storyIndex]; // You should have a model class called Story, not two arrays. UIImage *image = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURLString]]] autorelease]; // This is still way too slow if it's a remote URL UIImageView *imageView = (UIImageView*)[cell.contentView viewWithTag:TopImageTag]; imageView.image = image; return cell; } </code></pre> <p>I recommend you spend some quality time studying <a href="http://developer.apple.com/iphone/library/samplecode/TableViewSuite/" rel="nofollow noreferrer">TableViewSuite</a>, <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html" rel="nofollow noreferrer">Practical Memory Management</a>, and <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html" rel="nofollow noreferrer">Coding Guidelines for Cocoa</a>. Some time studying the basics of Cocoa would be useful as well, because the coding style here indicates that you may not have a solid foundation. Though it's a Mac book, I still recommend <a href="http://rads.stackoverflow.com/amzn/click/0321503619" rel="nofollow noreferrer">Cocoa Programming for Mac OS X</a>. If you're interested in using this to learn iPhone, I've put together a <a href="http://robnapier.net/blog/iphone-course-syllabus-158" rel="nofollow noreferrer">syllabus</a> that might help. I haven't reviewed it yet, but Stanford's online <a href="http://www.stanford.edu/class/cs193p/cgi-bin/index.php" rel="nofollow noreferrer">CS193P course</a> looks promising.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload