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;
}
该代码在iPhone模拟器中运行良好,但在真正的iPhone中,表视图在滚动时会卡住。
由于我知道重用可以解决这个问题,我也尝试了以下代码:
- (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;
}
使用上述代码后,滚动变得平滑,问题得到了解决,但提出的一个新问题是,重复的内容被随机显示,所有数据都被误导,例如,第一个单元格被显示3-4次,以此类推。。。
If anything is unclear feel free to ask friends. :)