I have been developing one application in iPhone and I'm making custom cells for a table view. Following is the code for cellForRowAtIndexPath.
- (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];
}
// Configure the cell...
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
NSDictionary *CurrentCommentRow = [comments objectAtIndex:indexPath.row];
NSString *Comment = [[CurrentCommentRow objectForKey:@"Comment"] objectForKey:@"text"];
NSString *FirstName = [[CurrentCommentRow objectForKey:@"F_Name"] objectForKey:@"text"];
NSString *LastName = [[CurrentCommentRow objectForKey:@"L_Name"] objectForKey:@"text"];
NSString *Date = [[CurrentCommentRow objectForKey:@"date"] objectForKey:@"text"];
NSString *Time = [[CurrentCommentRow objectForKey:@"Time"] objectForKey:@"text"];
NSString *UserImagePath = [[CurrentCommentRow objectForKey:@"UserImage"] objectForKey:@"text"];
NSString *TotalComments = (NSString*)[[CurrentCommentRow objectForKey:@"TotalComment"] objectForKey:@"text"];
NSString *imageRelativePath = [UserImagePath substringFromIndex:[UserImagePath rangeOfString:@"/" options:NSBackwardsSearch].location];
UIView *CommentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 297, 140)];
NSString* imageURL = [NSString stringWithFormat:@"http://primehangout.com/Images/IPhone%@",imageRelativePath];
NSURL* url = [NSURL URLWithString:imageURL];
HJManagedImageV *managedImage = [[HJManagedImageV alloc] initWithFrame:CGRectMake(10, 18, 56, 56)];
managedImage.url = url;
[self.objManager manage:managedImage];
[CommentView addSubview:managedImage];
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(82, 18, 180, 11)];
lblName.font = [UIFont boldSystemFontOfSize:12];
lblName.textColor = [UIColor colorWithRed:(5.0/255.0) green:(102.0/255.0) blue:(202.0/255.0) alpha:255];
lblName.text = [NSString stringWithFormat:@"%@:",FirstName];
[CommentView addSubview:lblName];
[lblName release];
UITextView *txtViewComment = [[UITextView alloc] initWithFrame:CGRectMake(73, 34, 180, 40)];
txtViewComment.contentInset = UIEdgeInsetsMake(-10,0,0,0);
[txtViewComment setEditable:FALSE];
txtViewComment.font = [UIFont fontWithName:@"Arial" size:11];
txtViewComment.text = Comment;
txtViewComment.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
[CommentView addSubview:txtViewComment];
[txtViewComment release];
UILabel *lblDateTime = [[UILabel alloc] initWithFrame:CGRectMake(82, 79, 180, 20)];
lblDateTime.font = [UIFont fontWithName:@"Arial" size:10];
lblDateTime.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
lblDateTime.text = [NSString stringWithFormat:@"%@ %@",Date,Time];
[CommentView addSubview:lblDateTime];
[lblDateTime release];
UILabel *lblTotalComments = [[UILabel alloc] initWithFrame:CGRectMake(82, 95, 80, 20)];
lblTotalComments.font = [UIFont fontWithName:@"Arial" size:11];
lblTotalComments.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
lblTotalComments.text = [NSString stringWithFormat:@"%@ Comments",TotalComments];
[CommentView addSubview:lblTotalComments];
[lblTotalComments release];
if ([TotalComments integerValue]>0)
{
UIButton *btnViewAll = [UIButton buttonWithType:UIButtonTypeCustom];
[btnViewAll setFrame:CGRectMake(140, 82, 70, 45)];
[btnViewAll setImage:[UIImage imageNamed:@"view_all_button_bg.png"] forState:UIControlStateNormal];
//[lblViewAll setTitle:@"view all" forState:UIControlStateNormal];
[btnViewAll addTarget:self action:@selector(ViewAllBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnViewAll setTag:(150 + indexPath.row)];
[CommentView addSubview:btnViewAll];
}
else {
[lblTotalComments setFrame:CGRectMake(82, 95, 150, 20)];
}
UIButton *btnAddComment = [UIButton buttonWithType:UIButtonTypeCustom];
[btnAddComment setFrame:CGRectMake(200, 82, 70, 45)];
[btnAddComment setImage:[UIImage imageNamed:@"add_comment.png"] forState:UIControlStateNormal];
[btnAddComment addTarget:self action:@selector(ViewAllBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnAddComment setTag:(1050 + indexPath.row)];
[CommentView addSubview:btnAddComment];
[cell.contentView addSubview: CommentView];
return cell;
}
The code works well in iPhone simulator but the tableview stucks at scrolloing in a real iPhone.
As I know that reusing can solve this problem I have tried out following code too:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
NSDictionary *CurrentCommentRow = [comments objectAtIndex:indexPath.row];
NSString *Comment = [[CurrentCommentRow objectForKey:@"Comment"] objectForKey:@"text"];
NSString *FirstName = [[CurrentCommentRow objectForKey:@"F_Name"] objectForKey:@"text"];
NSString *LastName = [[CurrentCommentRow objectForKey:@"L_Name"] objectForKey:@"text"];
NSString *Date = [[CurrentCommentRow objectForKey:@"date"] objectForKey:@"text"];
NSString *Time = [[CurrentCommentRow objectForKey:@"Time"] objectForKey:@"text"];
NSString *UserImagePath = [[CurrentCommentRow objectForKey:@"UserImage"] objectForKey:@"text"];
NSString *TotalComments = (NSString*)[[CurrentCommentRow objectForKey:@"TotalComment"] objectForKey:@"text"];
NSString *imageRelativePath = [UserImagePath substringFromIndex:[UserImagePath rangeOfString:@"/" options:NSBackwardsSearch].location];
UIView *CommentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 297, 140)];
NSString* imageURL = [NSString stringWithFormat:@"http://primehangout.com/Images/IPhone%@",imageRelativePath];
NSURL* url = [NSURL URLWithString:imageURL];
HJManagedImageV *managedImage = [[HJManagedImageV alloc] initWithFrame:CGRectMake(10, 18, 56, 56)];
managedImage.url = url;
[self.objManager manage:managedImage];
[CommentView addSubview:managedImage];
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(82, 18, 180, 11)];
lblName.font = [UIFont boldSystemFontOfSize:12];
lblName.textColor = [UIColor colorWithRed:(5.0/255.0) green:(102.0/255.0) blue:(202.0/255.0) alpha:255];
lblName.text = [NSString stringWithFormat:@"%@:",FirstName];
[CommentView addSubview:lblName];
[lblName release];
UITextView *txtViewComment = [[UITextView alloc] initWithFrame:CGRectMake(73, 34, 180, 40)];
txtViewComment.contentInset = UIEdgeInsetsMake(-10,0,0,0);
[txtViewComment setEditable:FALSE];
txtViewComment.font = [UIFont fontWithName:@"Arial" size:11];
txtViewComment.text = Comment;
txtViewComment.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
[CommentView addSubview:txtViewComment];
[txtViewComment release];
UILabel *lblDateTime = [[UILabel alloc] initWithFrame:CGRectMake(82, 79, 180, 20)];
lblDateTime.font = [UIFont fontWithName:@"Arial" size:10];
lblDateTime.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
lblDateTime.text = [NSString stringWithFormat:@"%@ %@",Date,Time];
[CommentView addSubview:lblDateTime];
[lblDateTime release];
UILabel *lblTotalComments = [[UILabel alloc] initWithFrame:CGRectMake(82, 95, 80, 20)];
lblTotalComments.font = [UIFont fontWithName:@"Arial" size:11];
lblTotalComments.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
lblTotalComments.text = [NSString stringWithFormat:@"%@ Comments",TotalComments];
[CommentView addSubview:lblTotalComments];
[lblTotalComments release];
if ([TotalComments integerValue]>0)
{
UIButton *btnViewAll = [UIButton buttonWithType:UIButtonTypeCustom];
[btnViewAll setFrame:CGRectMake(开发者_运维技巧140, 82, 70, 45)];
[btnViewAll setImage:[UIImage imageNamed:@"view_all_button_bg.png"] forState:UIControlStateNormal];
//[lblViewAll setTitle:@"view all" forState:UIControlStateNormal];
[btnViewAll addTarget:self action:@selector(ViewAllBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnViewAll setTag:(150 + indexPath.row)];
[CommentView addSubview:btnViewAll];
}
else {
[lblTotalComments setFrame:CGRectMake(82, 95, 150, 20)];
}
UIButton *btnAddComment = [UIButton buttonWithType:UIButtonTypeCustom];
[btnAddComment setFrame:CGRectMake(200, 82, 70, 45)];
[btnAddComment setImage:[UIImage imageNamed:@"add_comment.png"] forState:UIControlStateNormal];
[btnAddComment addTarget:self action:@selector(ViewAllBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnAddComment setTag:(1050 + indexPath.row)];
[CommentView addSubview:btnAddComment];
[cell.contentView addSubview: CommentView];
}
return cell;
}
After using above code the the scrolling becomes smooth and problem got solved but a new problem raised is that the duplicate content is being shown at random and all data get misguided for eg. the first cell is being shown 3-4 times and so on...
If anything is unclear feel free to ask friends. :)
reusing is the way to go, but you are doing it wrong.
Of course you have to set the new values after you've reused a cell.
if (cell == nil) {
// create new cell
cell = ...
UILabel *lblTotalComments = [[UILabel alloc] initWithFrame:CGRectMake(82, 95, 80, 20)];
lblTotalComments.tag = 192;
lblTotalComments.font = [UIFont fontWithName:@"Arial" size:11];
lblTotalComments.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
[CommentView addSubview:lblTotalComments];
[lblTotalComments release];
}
// configure cell
UILabel *lblTotalComments = (UILabel *)[cell viewWithTag:192];
lblTotalComments.text = [NSString stringWithFormat:@"%@ Comments",TotalComments];
you should get the idea how to implement this.
Are you getting the image from Internet every time? Why don't you store it in a imagecollection?
精彩评论