Ios 在UITableViewCell中放置UIProgressView时出现问题
我在Ios 在UITableViewCell中放置UIProgressView时出现问题,ios,uitableview,uiprogressview,Ios,Uitableview,Uiprogressview,我在UITableViewCell中放置了一个UIProgressView。我面临两个问题: 1. UITableView滚动受到阻碍。 2. UIProgressViewsetProgress每次都会被调用(显然),它会在每次出现时设置其进度 可以理解的是,问题1的出现是因为问题2的出现。为了测试这一点,我在UITableViewCell中注释了UIProgressView。滚动的速度太快了。 这是UIProgressView的代码: - (UITableViewCell *)tableVie
UITableViewCell
中放置了一个UIProgressView
。我面临两个问题:1. <代码>UITableView滚动受到阻碍。
2.
UIProgressView
setProgress每次都会被调用(显然),它会在每次出现时设置其进度
可以理解的是,问题1的出现是因为问题2的出现。为了测试这一点,我在UITableViewCell
中注释了UIProgressView
。滚动的速度太快了。这是
UIProgressView
的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
SWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
if ([cell respondsToSelector:@selector(layoutMargins)]) {
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = false;
}
}
else
{
NSArray *arr = [cell.contentView subviews];
for(int i=0; i<[arr count]; i++)
{
UIView *view = [arr objectAtIndex:i];
[view removeFromSuperview];
}
}
CustomerArticleDetail *customerArticleDetails = [arrDetails objectAtIndex:indexPath.row];
...
...
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
[progressView setProgressTintColor:[UIColor colorWithRed:31.0/255 green:177.0/255 blue:137.0/255 alpha:1.0]];
[[progressView layer]setCornerRadius:5.0f];
[[progressView layer]setBorderWidth:2.0f];
[[progressView layer]setMasksToBounds:TRUE];
progressView.clipsToBounds = YES;
[[progressView layer]setFrame:CGRectMake(50, 93, self.view.frame.size.width - 55, 8)];[[progressView layer]setBorderColor:[UIColor whiteColor].CGColor];
progressView.trackTintColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0];
[progressView setProgress: ([customerArticleDetails.Logged_Hours floatValue]/[customerArticleDetails.Planned_Hours floatValue]) animated:YES];
[cell.contentView addSubview:progressView];
...
...
return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
SWTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
cell=[[SWTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
cell.backgroundColor=[UIColor clearColor];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.accessoryType=UITableViewCellAccessoryNone;
if([单元格响应选择器:@selector(layoutMargins)]){
cell.layoutMargins=UIEdgeInsetsZero;
cell.preservesPerViewLayoutMargins=false;
}
}
其他的
{
NSArray*arr=[cell.contentView子视图];
对于(int i=0;i每次需要渲染UIView时,在单元格中添加UIView总是禁止的,也是错误的
这样就删除了可重用单元格的目的,而滚动是可怕的。
每次检查所有子视图并删除所有子视图的成本甚至更高。
这样做的方式是创建一次进度视图,然后根据需要刷新其进度
这有两种方式
如果您有自己的化妆间:
在单元格中创建UIProgress视图,只需调用set progress:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
SWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
if ([cell respondsToSelector:@selector(layoutMargins)]) {
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = false;
}
}
// Animated YES is maybe a fine idea to be more nice, but it could be better if you use NO.
[cell.progressView setProgress: ([customerArticleDetails.Logged_Hours floatValue]/[customerArticleDetails.Planned_Hours floatValue]) animated:YES];
}
如果您不拥有单元格,并且需要使用此方法创建视图
大概是这样的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
SWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
if ([cell respondsToSelector:@selector(layoutMargins)]) {
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = false;
}
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
[progressView setProgressTintColor:[UIColor colorWithRed:31.0/255 green:177.0/255 blue:137.0/255 alpha:1.0]];
[[progressView layer]setCornerRadius:5.0f];
[[progressView layer]setBorderWidth:2.0f];
[[progressView layer]setMasksToBounds:TRUE];
progressView.clipsToBounds = YES;
[[progressView layer]setFrame:CGRectMake(50, 93, self.view.frame.size.width - 55, 8)];[[progressView layer]setBorderColor:[UIColor whiteColor].CGColor];
progressView.trackTintColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0];
[progressView setTeg:10];
[cell.contentView addSubview:progressView];
}
UIProgressView *progressView = [cell.contentView viewWithTag:0]
// Animated YES is maybe a fine idea to be more nice, but it could be better if you use NO.
[progressView setProgress: ([customerArticleDetails.Logged_Hours floatValue]/[customerArticleDetails.Planned_Hours floatValue]) animated:YES];
}
这样就可以创建一次UIProgressview(因此没有滚动问题)只需在每个单元格上分别设置进度。如果您使用的是故事板,请将progressview添加到SWTableViewCell并将其导出。这样您就不必始终在代码中分配init UIProgressView。progressview层的初始化和更新使其滚动速度变慢。