【问题标题】:Hide unhide UITableViewCell background隐藏取消隐藏 UITableViewCell 背景
【发布时间】:2014-01-07 09:23:08
【问题描述】:

我有UITableView,我想在viewDidLoad 方法中在第一个单元格上添加背景图像,在第一个单元格上添加图像后,当用户选择任何其他行时,我想隐藏我的背景图像。

可以吗?

请提前帮助和感谢。

编辑:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
    if(indexPath.row==0){
        cell.backgroundView = [ [UIImageView alloc] initWithImage:[[UIImage imageNamed:@"active-tab.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
                flag=true;
         NSLog(@"willDisplayCell");
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (flag==true) {
       cell.backgroundView = nil; //How to get `cell` here ?
       //How to remove BGImage from first cell ???
    }
}

【问题讨论】:

  • 你应该包含更多的代码,让人们更容易看到你是怎么做的,而不是猜测
  • 不要忘记标记最佳答案并为对您有帮助的答案投票。面临同样问题的人会想知道是什么解决了您的问题,而那些花时间回答您问题的人应该获得声誉积分
  • 我删除了我的反对票,纯粹是因为添加了有助于识别问题的代码。然而,我确实觉得这个问题仍然有点缺乏,下次我会鼓励你付出更多的努力,因为其他人在看到这个问题时更有可能关闭一个问题

标签: ios iphone uitableview uiimage


【解决方案1】:

看看这个问题,第二个答案给出了很好的描述。

简而言之,您不应该使用 viewDiLoad 回调,而是使用

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }

从这里您可以根据需要自定义每个单元格的背景,只需在用户单击时重新加载行。

How to customize the background color of a UITableViewCell?

编辑

现在因为您添加了代码,我可以清楚地看到问题:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BTSTicketsCellIdentifier";
    CRHomeCategCell *cell = (CRHomeCategCell *)[_tblCateg dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.backgroundView = nil;

}

这并不像你认为的那样。 dequeueReusableCellWithIdentifier:CellIdentifier 为您提供了一个基于标识符标识的单元格的新实例。

您没有在此处获得对该行的引用,您正在创建一个新行并将其背景设置为 nil。

你的代码应该是这样的:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
    if(indexPath.row==0){
        if(cell.backgroundView == nil)
        {
            cell.backgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"active-tab.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
            NSLog(@"willDisplayCell");
        }
        else
        {
            cell.backgroundView = nil;
            NSLog(@"willHideCell");
        }

    }
}

这不是一个很好的解决方案,我个人会做一些更像是让这个自定义单元格保存一个布尔值并切换它的状态并检查它。但这取决于你来开发,这是它应该如何工作的总体思路。

编辑 2:

既然您决心在didSelectRowAtIndexPath 内运行它,并且完全无法进行任何级别的研究或为您的工作付出任何努力,我建议您使用以下方法:

tableView cellForRowAtIndexPath:<#(NSIndexPath *)#>

【讨论】:

  • willDisplayCell 将在第一个单元格上添加图像,但是当用户选择其他行时如何删除该图像。
  • 正如我在回答中提到的,当用户选择行时,重新加载单元格并再次触发上述回调,从那里您可以检查它是否正确的单元格然后再次修改它跨度>
  • [tableview reloadRowsAtIndexPaths: withRowAnimation:] 将重新加载特定行,[tableview reloadData] 将重新加载所有行。听起来您需要更好地阅读文档
  • 在我的问题中检查 EDIT
  • 我的日志在启动时显示willHideCell,但它应该调用willDisplayCell bocz 我想在启动时添加图像
【解决方案2】:

它可以在你的班级中添加表格视图代表

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
     *)indexPath  
    {  

     if((indexPath.row)==0)  {
      cell.backgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 
    }

    }


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

yourcustomcell *cell = [tableView cellForRowAtIndexPath:indexPath];
          cell.backgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"pressed.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 

or

      cell.backgroundView = nil; 

[tableview reloadData];

    }

【讨论】:

  • 如何在didSelectRowAtIndexPath中获取cell
  • UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; @Krunal
  • 我写了这个但没有帮助,我有自定义 UITablviewCell,- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.backgroundView = nil; }
  • 改为你的customcell而不是UItableviewcell @Krunal
  • 试过这个但还是有问题static NSString *CellIdentifier = @"BTSTicketsCellIdentifier"; CRHomeCategCell *cell = (CRHomeCategCell *)[_tblCateg dequeueReusableCellWithIdentifier:CellIdentifier]; cell.backgroundView = nil;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
相关资源
最近更新 更多