2014-01-07 166 views
0

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

可以吗?

请帮助和提前致谢。

编辑:

- (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 ??? 
    } 
} 
+1

你应该包括更多的代码,使我让人们更容易看到你试图做到这一点而不是猜测 –

+0

不要忘记标记最佳答案,并提出帮助你的答案。面临同样问题的人会想知道什么修复了你的问题,而那些花时间回答你的问题的人应该得到声望点 –

+0

我删除了我的downvote,纯粹是因为添加了代码以帮助确定问题。然而,我觉得这个问题还是有点欠缺,下次我会鼓励你把尽可能别人会更容易关闭眼看 –

回答

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为您提供基于标识符标识的单元格的新实例。

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

你的代码应该是更多的东西是这样的:

- (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 *)#> 
+0

'willDisplayCell'将增加在第一个单元格,但如何像当问题再努力一点当用户选择其他行时删除该图像。 – Krunal

+0

正如我在答复中提到,当用户选择该行,重新装入电池及以上的回调将再次触发,从那里你可以检查其右边的单元格,然后再次进行修改 –

+0

如何重装电池? – Krunal

0

它的作品添加表视图代表你的类

- (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]; 

    } 
+0

如何在'didSelectRowAtIndexPath'中获取'cell'? – Krunal

+0

UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; @Krunal – codercat

+0

我写了这一点,但没有帮助,我有自定义UITablviewCell, ' - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath { 的UITableViewCell *电池=的tableView的cellForRowAtIndexPath:indexPath ]。 cell.backgroundView = nil; }' – Krunal