我一直在iPhone中开发一个应用程序,我正在为表视图制作自定义单元格。 以下是cellForRowAtIndexPath的代码。TableView卡在iPhone中的滚动,但在模拟器中效果不错
- (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 scrolloing的实现代码如下stucks。
据我知道,重用能解决这个问题,我已经尝试了下面的代码太:
- (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次,依此类推...
如果有什么不清楚的地方,请随时问问朋友。 :)
哇。我完全错过了这部分。这当然比真实。 – 2011-02-17 14:13:17