ios - 使用可重用单元格在 iPhone 上缓慢加载 tableview
<p><p>我从 Parse.com 后端加载数据,他们向我发送了使用可重用单元格的解决方案,但现在我仍然遇到加载速度问题,这是我在 tableview 中的编码,我有一个用于制作的子类我的单元格 (ExploreStreamCustomCell.m)</p>
<pre><code> - (ExploreStreamCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
object:(PFObject *)object
{
static NSString *CellIdentifier = @"ExploreStreamCustomCell";
ExploreStreamCustomCell *cell = ;
if (cell == nil) {
cell = [ initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
// Configure the cell
cell.listItemTitle.text = ;
cell.checkinsLabel.text = ];
cell.descriptionLabel.text = ;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
PFFile *listThumbnail = ;
cell.listViewImage.image = ; // placeholder image
cell.listViewImage.file = listThumbnail;
;
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = ;
cell.textLabel.font = ;
return cell;
}
</code></pre>
<p>如果我在单元格 == nil 中有//configure 单元格的所有内容,它会很快,但它会显示 9 个唯一数据行中的 3 个,并重复这 3 个唯一内容单元格 3 次? </p>
<p>在 ExploreStreamCustomCell.m 中编辑额外代码</p>
<pre><code>#import "ExploreStreamCustomCell.h"
@implementation ExploreStreamCustomCell
@synthesize listViewImage,
iconLocation,
iconPeople,
iconCheckins,
listItemTitle,
locationLabel,
peopleLabel,
checkinsLabel,
descriptionLabel,
listItemView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if(self = ){
//Initialization code
listItemView = [ init];
listViewImage = [ init];
iconLocation = [ init];
iconPeople = [ init];
iconCheckins = [ init];
listItemTitle = [ init];
locationLabel = [ init];
peopleLabel = [ init];
checkinsLabel = [ init];
descriptionLabel = [ init];
listViewImage.image = ;
iconLocation.image = ;
iconPeople.image = ;
iconCheckins.image = ;
listItemTitle.text = @"text";
locationLabel.text = @"0,7 km";
peopleLabel.text = @"34";
checkinsLabel.text = @"61";
descriptionLabel.text = @"Description text.";
;
;
;
;
;
;
;
;
;
;
}
return self;
}
- (void)layoutSubviews {
;
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+0 , 33, 280, 124);
listViewImage.frame = frame;
listViewImage.contentMode = UIViewContentModeScaleAspectFill;
listViewImage.layer.masksToBounds = YES;
//listViewImage.backgroundColor = ;
frame= CGRectMake(boundsX+20 , 164, 12, 18);
iconLocation.frame = frame;
//iconLocation.backgroundColor = ;
frame= CGRectMake(boundsX+102 , 164, 24, 18);
iconPeople.frame = frame;
//iconPeople.backgroundColor = ;
frame= CGRectMake(boundsX+193 , 164, 20, 16);
iconCheckins.frame = frame;
//iconLocation.backgroundColor = ;
frame= CGRectMake(boundsX+0 , 0, 280, 33);
listItemView.frame = frame;
listItemView.backgroundColor = ;
frame= CGRectMake(boundsX+20 , 3, 240, 29);
listItemTitle.frame = frame;
//listItemTitle.textColor = ;
listItemTitle.textAlignment = UITextAlignmentLeft;
listItemTitle.font = ;
listItemTitle.textColor = ;
listItemTitle.backgroundColor = ;
listItemTitle.lineBreakMode = UILineBreakModeTailTruncation;
//listItemTitle.backgroundColor = ;
frame= CGRectMake(boundsX+40 , 164, 57, 21);
locationLabel.frame = frame;
locationLabel.textAlignment = UITextAlignmentLeft;
locationLabel.font = ;
locationLabel.textColor = ;
locationLabel.backgroundColor = ;
locationLabel.lineBreakMode = UILineBreakModeTailTruncation;
locationLabel.numberOfLines = 1;
//locationLabel.backgroundColor = ;
frame= CGRectMake(boundsX+134 , 164, 57, 21);
peopleLabel.frame = frame;
peopleLabel.textAlignment = UITextAlignmentLeft;
peopleLabel.font = ;
peopleLabel.textColor = ;
peopleLabel.backgroundColor = ;
peopleLabel.lineBreakMode = UILineBreakModeTailTruncation;
peopleLabel.numberOfLines = 1;
frame= CGRectMake(boundsX+221 , 164, 51, 21);
checkinsLabel.frame = frame;
checkinsLabel.textAlignment = UITextAlignmentLeft;
checkinsLabel.font = ;
checkinsLabel.textColor = ;
checkinsLabel.backgroundColor = ;
checkinsLabel.lineBreakMode = UILineBreakModeTailTruncation;
checkinsLabel.numberOfLines = 1;
frame= CGRectMake(boundsX+0 , 189, 280, 55);
descriptionLabel.frame = frame;
descriptionLabel.textAlignment = UITextAlignmentLeft;
descriptionLabel.font = ;
descriptionLabel.backgroundColor = ;
descriptionLabel.lineBreakMode = UILineBreakModeTailTruncation;
descriptionLabel.numberOfLines = 3;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
;
// Configure the view for the selected state
}
/*
- (id)initWithFrame:(CGRect)frame
{
self = ;
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>探索代码效率低下的一个好方法是使用 Instruments 的 Time Profiler 工具。 Time Profiler 可以让您在代码中逐行查看每个任务花费了多少时间。</p>
<p> <img src="/image/grH9B.png" alt="Time Profiler"/> </p>
<hr/>
<p>我会推荐以下配置设置:</p>
<p> <img src="/image/VNJKT.png" alt="Profiler settings"/> </p>
<hr/>
<p>来自 Apple 的人脸检测示例应用:</p>
<p> <img src="/image/OAxjx.png" alt="Face Detection"/> </p>
<hr/>
<p>然后您可以双击任何一行(百分比越高意味着该方法调用所花费的时间越多),以在代码中查看每个地方花费了多少时间。</p>
<p> <img src="/image/OikJH.png" alt="Code analysis"/> </p>
<hr/>
<p>从这里您可以开始找出您效率低下的地方,并准确了解是什么占用了这么多时间。祝你好运!</p></p>
<p style="font-size: 20px;">关于ios - 使用可重用单元格在 iPhone 上缓慢加载 tableview,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/11494914/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/11494914/
</a>
</p>
页:
[1]