UITableView单元格突出显示/保留所选值IOS



我有UIView(左菜单)与UITableView (UITableViewStylePlain) .我有大约 7 个控制器,在选择每个单元格时我想推送相应的控制器。我尝试为黄色单元格自定义突出显示,如下所示,

  UIColor *yel=[[UIColor alloc]initWithRed:240/255.0 green:197/255.0 blue:67/255.0 alpha:1.0];
  UIView *bgColorView = [[UIView alloc] init];
  [bgColorView setBackgroundColor:yel];
  [cell setSelectedBackgroundView:bgColorView];

cellForRowAtIndexPath.但是如果我移动到下一个控制器,我无法保留选定的单元格。在didSelectRowAtIndexPath中,我正在捕获最后选择的索引(当我选择新单元格时,旧的应该不突出显示)。但似乎,如果我定制它不会保留。如果我保持UITableViewCellSelectionStyleNonecell.backgroundColor它有效。但不突出:(

  [[NSUserDefaults standardUserDefaults]setInteger:indexPath.row forKey:@"SSLastSelectedLeftMenuIndex"];

我正在使用框架初始化UIView(左菜单)。

问题:几分钟后突出显示灰色自定义黄色突出显示并且不保留选定的单元格颜色。

我知道我错过了一些愚蠢的东西。但它消耗了我的时间。提前感谢:)

更新:-以下是我的-cellForRowAtIndexpath方法

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if([[NSUserDefaults standardUserDefaults]integerForKey:@"SSLastSelectedLeftMenuIndex"]==indexPath.ro‌w)
{
    ///<HIGHLIGHT CODE>;
}

选择最后选择的单元格..(尝试)

- (void)viewWillAppear
   {
       [uper viewWillAppear];
       //hear u can set the selected cell
       //get indexpath of row
       int k = your saved row
       NSIndexPAth *path = [NSIndexPath indexPathWithIndex:k];//if u hav single section or u can use other class method
      [tableView selectRowAtIndexPath:_selctedIndex animated:NO scrollPosition: UITableViewScrollPositionNone];//hear u are directly setting the last selected cell once view will appear
   }

跳这个有助于你:)

如果您使用的是UITableViewCell,那么您会做这样的事情,对于黄色选择单元格

     //in controller do like this
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if(cell == nil)
        {
           cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
          UIColor *yel=[[UIColor alloc]initWithRed:240/255.0 green:197/255.0 blue:67/255.0 alpha:1.0];
          UIView *bgColorView = [[UIView alloc] init];
         [bgColorView setBackgroundColor:yel];
         [cell setSelectedBackgroundView:bgColorView];
       }
      return cell;
   }

在子类化表格单元格中,有一种方法可以显示黄色以选择单元格

  //in CustomCell.m
  //in the custom cell set the color for selected state
  //override this method
   - (void)setSelected:(BOOL)selected animated:(BOOL)animated
   {
        [super setSelected:selected animated:animated];
         // Configure the view for the selected state
         if(selected)
         {
             //same code of urs
             UIColor *yel=[[UIColor alloc]initWithRed:240/255.0 green:197/255.0 blue:67/255.0 alpha:1.0];
             UIView *bgColorView = [[UIView alloc] initWithFrame:self.bounds];//for entaire cell,set the frame
             [bgColorView setBackgroundColor:yel];
             [self setSelectedBackgroundView:bgColorView];
         }
     }

您误解了选择并突出显示了一个单元格,您应该像下面一样更改单元格以选择以前选择的项目。

在此之前声明一个 NSIndexpath 类型的 globle 变量_selctedIndex来存储最后一个选定的单元格

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        UIColor *yel=[[UIColor alloc]initWithRed:240/255.0 green:197/255.0 blue:67/255.0 alpha:1.0];
        UIView *bgColorView = [[UIView alloc] init];
        [bgColorView setBackgroundColor:yel];
        [cell setSelectedBackgroundView:bgColorView];
    }
    if([[NSUserDefaults standardUserDefaults]integerForKey:@"SSLastSelectedLeftMenuIndex"]==indexPath.row)
    {
        //<HIGHLIGHT CODE>;
        _selctedIndex=indexPath;   //code Updated
    }
return cell;
}

重新加载表视图后,调用以下代码行

[tableView selectRowAtIndexPath:_selctedIndex animated:NO scrollPosition:UITableViewScrollPositionTop];
    }

类会将您提到的视图分配给属性 selectedBackgroundView,而不是默认蓝色视图,以便在选择单元格时显示在背景中

我在项目中遇到了完全相同的问题,当点击一个单元格足够长的时间以参与突出显示然后移动到滚动时,UITableView 以某种方式保留突出显示的索引,以便下一次尝试选择使用以前的 indexPath。

为了解决我做了以下事情

添加属性以跟踪违规索引路径

...
@property (nonatomic,strong) NSIndexPath *cachedPath;
...

Track indexPath with didUnhighlightRowAtIndexPath

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.cachedPath = indexPath;
}

在拖动时重新加载有问题的索引路径

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (self.cachedPath)
    {
        [self.tableView reloadRowsAtIndexPaths:@[self.cachedPath] withRowAnimation:UITableViewRowAnimationNone];
    }
    self.cachedPath = nil;
}

为了良好的措施,在选择时清除属性

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.cachedPath = nil;
}

最新更新