Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TableView在IOS中追加更多数据时闪烁_Ios_Iphone_Tableview_Heightforrowatindexpath - Fatal编程技术网

TableView在IOS中追加更多数据时闪烁

TableView在IOS中追加更多数据时闪烁,ios,iphone,tableview,heightforrowatindexpath,Ios,Iphone,Tableview,Heightforrowatindexpath,我涉及到TableView闪烁问题。当我通过loadmore在tableview中追加数据时,tableview正在闪烁。如果我使用这个代码,那么一切都很好 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 400; } 但我在计算每行的高度。因为每行的高度不一样。然后我正在使用该代码,在应用该代码之后,我的TableView正在闪烁 -(

我涉及到TableView闪烁问题。当我通过loadmore在tableview中追加数据时,tableview正在闪烁。如果我使用这个代码,那么一切都很好

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 400;
}
但我在计算每行的高度。因为每行的高度不一样。然后我正在使用该代码,在应用该代码之后,我的TableView正在闪烁

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *temp;

if(_buddyWallSection)
    temp =  arrayBuddyList;
else
    temp = arrayMyWallList;

CGFloat height = 320;
if( [[temp objectAtIndex:indexPath.row/2] isKindOfClass:[NSString class]] )
{
    return height;
}
CGSize maximumLabelSize = CGSizeMake(280,9999);
GolferWall *wallComnt = [temp objectAtIndex:indexPath.row/2];
CGSize expectedSize = [[self plainTextFromHTMLText:wallComnt.contentTextHTML]sizeWithFont:[UIFont fontWithName:FontNameArial size:15] constrainedToSize:maximumLabelSize  lineBreakMode:NSLineBreakByWordWrapping];
height = expectedSize.height;

NSLog(@"expected size %f", height);

if(wallComnt.imageNames.count == 0)
   return 130 + ( height);

if ( height < 100 )
{
    height = 350;
}
else
    height = height * 2 ;

return height;
}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath
{
NSMutableArray*温度;
如果(_buddywall部分)
temp=arrayBuddyList;
其他的
temp=ArrayWallList;
CGFloat高度=320;
如果([[temp objectAtIndex:indexath.row/2]是kindofClass:[NSString class]])
{
返回高度;
}
CGSize maximumLabelSize=CGSizeMake(2809999);
GolferWall*wallComnt=[temp objectAtIndex:indexath.row/2];
CGSize expectedSize=[[self-plainTextFromHTMLText:wallComnt.contentTextHTML]sizeWithFont:[UIFont fontWithName:FontNameArial size:15]constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
高度=预期尺寸。高度;
NSLog(@“预期尺寸%f”,高度);
如果(wallComnt.imageNames.count==0)
返回130+(高度);
如果(高度<100)
{
高度=350;
}
其他的
高度=高度*2;
返回高度;
}
你能查一下我遗漏了什么吗


谢谢

这里的问题是,每次要显示一行时,您都在执行cpu密集型任务。更确切地说,问题出在下面这行:

CGSize expectedSize = [[self plainTextFromHTMLText:wallComnt.contentTextHTML]sizeWithFont:[UIFont fontWithName:FontNameArial size:15] 
constrainedToSize:maximumLabelSize  
lineBreakMode:NSLineBreakByWordWrapping];
解决方案是对每个对象预先评估该大小。例如,在GolferWall类中,可以添加如下属性:

@property (nonatonic, assign, readonly) CGFloat contentSize;
在GolferWall init方法中,您只需复制已经在tableView:heightForRowAtIndexPath:

- (instancetype) init
{
    self = [super init];
    *** your init stuff here ***
    self.contentSize = [[self plainTextFromHTMLText:self.contentTextHTML]sizeWithFont:[UIFont fontWithName:FontNameArial size:15] constrainedToSize:maximumLabelSize  lineBreakMode:NSLineBreakByWordWrapping];

    return self;
}
然后在tableView中:heightForRowAtIndexPath:只需替换以下代码:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *temp;

if(_buddyWallSection)
temp =  arrayBuddyList;
else
    temp = arrayMyWallList;

CGFloat height = 320;
if( [[temp objectAtIndex:indexPath.row/2] isKindOfClass:[NSString class]] )
{
    return height;
}
CGSize maximumLabelSize = CGSizeMake(280,9999);
GolferWall *wallComnt = [temp objectAtIndex:indexPath.row/2];

// The magic is here
CGSize expectedSize = wallComnt.contentSize;
height = expectedSize.height;

NSLog(@"expected size %f", height);

if(wallComnt.imageNames.count == 0)
   return 130 + ( height);

if ( height < 100 )
{
    height = 350;
}
else
    height = height * 2 ;

return height;
}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath
{
NSMutableArray*温度;
如果(_buddywall部分)
temp=arrayBuddyList;
其他的
temp=ArrayWallList;
CGFloat高度=320;
如果([[temp objectAtIndex:indexath.row/2]是kindofClass:[NSString class]])
{
返回高度;
}
CGSize maximumLabelSize=CGSizeMake(2809999);
GolferWall*wallComnt=[temp objectAtIndex:indexath.row/2];
//魔法就在这里
CGSize expectedSize=wallComnt.contentSize;
高度=预期尺寸。高度;
NSLog(@“预期尺寸%f”,高度);
如果(wallComnt.imageNames.count==0)
返回130+(高度);
如果(高度<100)
{
高度=350;
}
其他的
高度=高度*2;
返回高度;
}

如您所见,uitableview中的每一行都会调用该方法,您正在执行cpu密集型任务:CGSize expectedSize=[[self-plainTextFromHTMLText:wallComnt.contentTextHTML]sizeWithFont:[UIFontWithName:FontNameArial size:15]constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];因此,我的建议是预先评估每一行的大小,并将该信息存储在一个可以从tableview:heightForRowAtIndexPath访问的对象中:您是否碰巧实现了willDisplayCell方法?@Lolloz89是的,因此这需要时间。我们有其他方法来修复它吗?@ZeMoon我从来没有使用过“willDisplayCell”这种方法@RahulRawat是的,正如我所说,你只能对每个高度评估一次,例如在viewDidLoad中。然后将该高度作为CGFloat存储在GolferWall的属性中。或者更好,您可以在GolferWall的init方法中计算该属性。