Table 滚动时查看内容丢失
Table view content missing during scrolling
我正在使用 table 视图单元格并在单击单元格中的按钮时添加弹出视图。但是当我滚动时,table 视图弹出视图从我的单元格中消失。这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellF
orRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =[NSString stringWithFormat:@"ArticleCell%d %d",indexPath.section,indexPath.row];
__block ArticleCell_iPad *cell = (ArticleCell_iPad *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ArticleCell_iPad alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[[NSBundle mainBundle] loadNibNamed:@"SynopsysViewSharing" owner:self options:nil];
cell = [[[ArticleCell_iPad alloc] initWithFrame:CGRectMake(0, 0, kCellWidth_iPad, kCellHeight_iPad)] autorelease];
__block NSDictionary *currentArticle = [self.articles objectAtIndex:indexPath.section];
[cell.iconImage loadImageWithURL:[[currentArticle objectForKey:@"sourceLogoImg"]objectForKey:@"text"]];
[cell.sideButton setBackgroundImage:[UIImage imageNamed:[cellButtonImageArr objectAtIndex:self.selectIndex]]
[cell.sideButton addTarget:self action:@selector(sideButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.sideButton.tag = indexPath.section;
TableViewCellSelectionStyleNone;
}
return cell;
}
按钮操作:
-(IBAction)sideButtonClicked:(UIButton*)sender
{
buttonShare=[[[NSBundle mainBundle] loadNibNamed:@"SynopsysViewSharing" owner:self options:nil] lastObject];
NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:sender.tag];
ArticleCell_iPad *cellSelected = (ArticleCell_iPad*)[_horizontalTableView cellForRowAtIndexPath:myIP];
[cellSelected.contentView addSubview:buttonShare];
buttonShare.alpha = 0.0f;
buttonShare.tag=500;
[buttonShare setFrame:CGRectMake(0,600, cellSelected.frame.size.height,55)];
[UIView animateWithDuration:0.5 animations:^{
[buttonShare setFrame:CGRectMake(0,cellSelected.frame.size.width-55, cellSelected.frame.size.height,55)];
buttonShare.alpha = 1.0f;
} completion:^(BOOL finished) {
}];
}
而不是通过编码将 buttonshare 添加到 contentview,请尝试在原型单元格本身的情节提要中添加 'buttonshare' 并通过检查隐藏 'buttonshare' 使其隐藏 属性.
然后点击 UITableviewCell 中的按钮,将 按钮的隐藏 属性 共享到 sideButtonClicked 中的 "NO":
希望对您有所帮助:)
你的实现有两个主要错误:
- 在您的
cellForRowAtIndexPath
中,您需要为给定的 indexPath 初始化单元格,即使它是重复使用的单元格(如果 cell != nil
)。
- 在您的
sideButtonClicked
中,执行 而不是 调用 cellForRowAtIndexPath
以找到单元格。它将创建一个全新的单元格,而不是返回屏幕上的单元格。改为从方法的 UIButton
参数导出它是哪一行。
我正在使用 table 视图单元格并在单击单元格中的按钮时添加弹出视图。但是当我滚动时,table 视图弹出视图从我的单元格中消失。这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellF
orRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =[NSString stringWithFormat:@"ArticleCell%d %d",indexPath.section,indexPath.row];
__block ArticleCell_iPad *cell = (ArticleCell_iPad *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ArticleCell_iPad alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[[NSBundle mainBundle] loadNibNamed:@"SynopsysViewSharing" owner:self options:nil];
cell = [[[ArticleCell_iPad alloc] initWithFrame:CGRectMake(0, 0, kCellWidth_iPad, kCellHeight_iPad)] autorelease];
__block NSDictionary *currentArticle = [self.articles objectAtIndex:indexPath.section];
[cell.iconImage loadImageWithURL:[[currentArticle objectForKey:@"sourceLogoImg"]objectForKey:@"text"]];
[cell.sideButton setBackgroundImage:[UIImage imageNamed:[cellButtonImageArr objectAtIndex:self.selectIndex]]
[cell.sideButton addTarget:self action:@selector(sideButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.sideButton.tag = indexPath.section;
TableViewCellSelectionStyleNone;
}
return cell;
}
按钮操作:
-(IBAction)sideButtonClicked:(UIButton*)sender
{
buttonShare=[[[NSBundle mainBundle] loadNibNamed:@"SynopsysViewSharing" owner:self options:nil] lastObject];
NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:sender.tag];
ArticleCell_iPad *cellSelected = (ArticleCell_iPad*)[_horizontalTableView cellForRowAtIndexPath:myIP];
[cellSelected.contentView addSubview:buttonShare];
buttonShare.alpha = 0.0f;
buttonShare.tag=500;
[buttonShare setFrame:CGRectMake(0,600, cellSelected.frame.size.height,55)];
[UIView animateWithDuration:0.5 animations:^{
[buttonShare setFrame:CGRectMake(0,cellSelected.frame.size.width-55, cellSelected.frame.size.height,55)];
buttonShare.alpha = 1.0f;
} completion:^(BOOL finished) {
}];
}
而不是通过编码将 buttonshare 添加到 contentview,请尝试在原型单元格本身的情节提要中添加 'buttonshare' 并通过检查隐藏 'buttonshare' 使其隐藏 属性.
然后点击 UITableviewCell 中的按钮,将 按钮的隐藏 属性 共享到 sideButtonClicked 中的 "NO":
希望对您有所帮助:)
你的实现有两个主要错误:
- 在您的
cellForRowAtIndexPath
中,您需要为给定的 indexPath 初始化单元格,即使它是重复使用的单元格(如果cell != nil
)。 - 在您的
sideButtonClicked
中,执行 而不是 调用cellForRowAtIndexPath
以找到单元格。它将创建一个全新的单元格,而不是返回屏幕上的单元格。改为从方法的UIButton
参数导出它是哪一行。