繁体   English   中英

UITableView Cell突出显示/保留选定的值IOS

[英]UITableView Cell highlight / retain selected value IOS

我有UITableView (UITableViewStylePlain) UIView (LeftMenu 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 (LeftMenu)。

问题:几分钟后高亮显示灰色,自定义黄色高亮显示,并且未保留选定的单元格颜色。

我知道我缺少一些愚蠢的东西。 但是它消耗了我的时间。 预先感谢:)

更新:-下面是我的-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];
         }
     }


您对选择有误解,并突出显示一个单元格,应按如下所示更改cellForRowAtIndexPath来选择先前选择的项目。

在此之前声明一个NSIndexpath类型的全局变量_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之后,请调用以下代码行

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

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

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

为了解决我做了以下事情

添加属性以跟踪违规indexPath

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

使用didUnhighlightRowAtIndexPath跟踪indexPath

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

在拖动时重新加载有问题的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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM