hi all i implemented customized UITableViewcell with the below code.Each cell loaded with four images..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *hlCellID = @"hlCellID";
UITableViewCell *hlcell = [tableView dequeueReusableCellWithIdentifier:hlCellID];
if(hlcell == nil) {
hlcell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID] autorelease];
hlcell.accessoryType = UITableViewCellAccessoryNone;
hlcell.selectionStyle = UITableViewCellSelectionStyleNone;
[hlcell.contentView removeAllSubViews];
}
//NSLog(@"the scetions is %@",sections);
int section = indexPath.section;
for(int i=0;i<4;i++){
CGRect rect = CGRectMake(18+192*i, (4*indexPath.row)+50, 120, 150);
if ((4*indexPath.row)+i>=[self.imagesToDisplay count]) {
break;
}
UIImage *imageToDisplay=[UIImage imageWithData:[self.imagesToDisplay objectAtIndex:(4*indexPath.row)+i]];
NSLog(@"The size of the image is:%@",NSStringFromCGSize(imageToDisplay.size));
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
[button setBackgroundImage:imageToDisplay forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeCenter];
NSString *tagValue = [NSString stringWithFormat:@"%d%d",indexPath.row,i];
NSLog(@"the tag is %@",tagValue);
button.tag = [tagValue intValue];
NSLog(@"....tag....%d", button.tag);
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[hlcell.contentView addSubview:button];
[button release];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(18+192*i,(4*indexPath.row)+100+70 , 100, 100)] ;
label.text = @"price $0.99";
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont fontWithName:@"ArialMT" size:12];
[hlcell.contentView addSubview:label];
[label release];
}
return hlcell;
}
for each image acts as uibutton.i am trying to load around 1000 images.these images i am taking from the server.when one image is loaded to my app i am updating total cell.
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:ceil((float)[gridView.imagesToDisplay count]/4)-1 inSection:0];
NSArray *cellIndexPath=[NSArray arrayWithObjects:indexPath,nil];
[gridView.tableview reloadRowsAtIndexPaths:cellIndexPath withRowAnimation:UITableViewRowAnimationNone];
upto now my code is working fine and when i am trying to scroll many times after loading all the images its getting crash.and my GDB showing as memory warning.can any one suggest me why the issue happening.Thanks for your response in advance.
It looks like you always allocate new Images and display them on the Cell, but you never actually release the images when the cell is no longer displayed.
The definition for the UITableView cell states, that as soon a cell is no longer used it is purged and prepared for a new content. In your code you always add new subviews to the cell, but never actually release the content.
The best solution to your problem is to implement a UITableViewCell by subclassing it and manually set the images. Furthermore implement the
method of the class to release the images that are currently displayed. See the reference for more documentation of UITableViewCell