这是我的cellForRowAtIndexPath方法。在我的项目中使用ARC。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString* cellIdentifier = @"ActivityCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Activity* activityToShow = [self.allActivities objectAtIndex:indexPath.row];
//Cell and cell text attributes
cell.textLabel.text = [activityToShow name];
//Slowing down the list scroll, I guess...
LastWeekView* lastWeekView = [[LastWeekView alloc] initWithFrame:CGRectMake(10, 39, 120, 20)];
[lastWeekView setActivity:activityToShow];
lastWeekView.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lastWeekView];
return cell;
}
LastWeelView分配正在减慢滚动速度。在lastWeekView中,我从CoreData获取实体的关系,对这些值执行计算,并在其drawRect方法内绘制一些颜色。
这是LastWeekView的drawRect
- (void)drawRect:(CGRect)rect
{
NSArray* activityChain = self.activity.computeChain; //fetches its relationships data
for (id item in activityChain) {
if (marking == [NSNull null])
{
[notmarkedColor set];
}
else if([(NSNumber*)marking boolValue] == YES)
{
[doneColor set];
}
else if([(NSNumber*)marking boolValue] == NO)
{
[notdoneColor set];
}
rectToFill = CGRectMake(x, y, 10, 10);
CGContextFillEllipseInRect(context, rectToFill);
x = x + dx;
}
}
我该怎么做才能使tableView的滚动变得平滑?如果我必须将此lastWeekView异步添加到每个单元格的contentView中,该怎么办?请帮忙。
最佳答案
我建议在单元格的分配范围内分配LastWeekView
。另外-获取viewDidLoad
中的所有核心数据对象,以便在cellForRowAtIndexPath:
方法中从数组而不是从存储中检索它。它看起来应该像这样:
- (void)viewDidLoad
...
_activities = [Activity fetchAllInContext:managedObjectContext];
...
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCell];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
LastWeekView* lastWeekView = [[LastWeekView alloc] initWithFrame:CGRectMake(10, 39, 120, 20)];
lastWeekView.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lastWeekView];
}
Activity *activityToShow = [_activities objectAtIndex:[indexPath row]];
LastWeekView *lastWeekView = (LastWeekView *)[[[cell contentView] subviews] lastObject];
[lastWeekView setActivity:activityToShow];
return cell;
}
请注意,您也可以将UITableViewCell子类化,以用
LastWeekView
替换contentView,以快速访问activity属性。