【发布时间】:2013-12-06 05:16:41
【问题描述】:
在@parcs 和@payal 的帮助下找到了解决方案- 这个链接有帮助-> http://www.appcoda.com/customize-table-view-cells-for-uitableview/
基本上,当我滚动时,表格视图会滞后,我浏览了很多帖子,但没有帮助-
UITableView in UIScrollview scrolling delay
Delayed UIImageView Rendering in UITableView
UITableView lags while scrolling
这是我在 cellForRowIndexpath 中使用的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"WorkoutCell";
WorkoutCell *cell = (WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
cell=(WorkoutCell *)controller.view;
}
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//diplaying Workout Data
[cell setupFont];
cell.lblPoint.text=[NSString stringWithFormat:@"%@",[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"point"]];
cell.lblReps.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"reps"];
cell.lblWorkoutCategory.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"workout"];
cell.lblWeight.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"weight"];
cell.lblSets.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"sets"];
//Formatting Date
cell.lblPostedDate.text=[NSString stringWithFormat:@"Posted On: %@",[self formatDate:[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"time"]]];
return cell;
}
从 cellForRowIndexpath 调用的函数 SetupFont -
(void)setupFont
{
self.lblPoint.font=DEFAULT_FONT(13);
self.pointLblScreen.font=DEFAULT_FONT(13);
self.lblPostedDate.font=DEFAULT_FONT(13);
self.lblReps.font=DEFAULT_FONT(13);
self.lblSets.font=DEFAULT_FONT(13);
self.lblWeight.font=DEFAULT_FONT(13);
self.lblWorkoutCategory.font=DEFAULT_FONT(18);
self.lblWorkoutCategory.strokeColor=kStrokeColor2;
self.lblWorkoutCategory.strokeSize = kStrokeBigSize;
for(UILabel *lbl in [self subviews])
{
if(lbl.tag==9)
{
lbl.font=DEFAULT_FONT(13);
}
}
}
从 cellForRowIndexpath 调用的格式化日期函数
#pragma mark - Format Date Function
-(NSMutableString*)formatDate:(NSString*)date
{
NSMutableString *formattedDate=[[NSMutableString alloc]init];
NSUInteger p = 0;
NSUInteger length = [date length];
while ( p < length) {
unichar ch = [date characterAtIndex: p];
if (ch!=' ')
{
if(ch=='-')
{
ch='/';
}
NSString *appendString = [NSString stringWithFormat:@"%c",ch];
[formattedDate appendString:appendString];
p++;
}
else
{
break;
}
}
return formattedDate;
}
【问题讨论】:
-
这段代码最不寻常的方面(以及问题的可能原因)是分配一个视图控制器,然后将它的视图转换为一个 tableviewcell。通过注释掉所有其他配置步骤,您可以学到很多东西,也许只留下这样的一行:cell.lblPoint.text=[NSString stringWithFormat:@"%d", indexPath.row];
-
为什么要将视图控制器分配给单元格,这会使您的应用程序变慢..
-
@danh- 好吧,我在大多数应用程序中都使用过这个过程,但直到现在我还没有发现这种问题。
-
好的,我正在检查...我会告诉大家的。
-
@danh 和 Bullet Raja——我如何替换这段代码 if(!cell) { UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];单元格=(WorkoutCell *)controller.view; }
标签: ios iphone objective-c uitableview