[英]UITableView Cell highlight / retain selected value IOS
I have UIView
(LeftMenu) with UITableView (UITableViewStylePlain)
. 我有
UITableView (UITableViewStylePlain)
UIView
(LeftMenu UITableView (UITableViewStylePlain)
。 I have around 7 controllers , Where on selection of each cell I wanna push corresponding controller. 我大约有7个控制器,在选择每个单元格时,我想推送相应的控制器。 I tried custom highlight for cell with yellow color as below ,
我尝试使用黄色的单元格自定义突出显示,如下所示,
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];
in cellForRowAtIndexPath
. 在
cellForRowAtIndexPath
。 But I am unable to retain selected cell If I move to next controller. 但是如果我移到下一个控制器,我将无法保留所选的单元格。 In
didSelectRowAtIndexPath
, I am capturing last selected index ( When I select new cell , old should be unhighlighted). 在
didSelectRowAtIndexPath
,我正在捕获最后选择的索引(当我选择新单元格时,应该取消突出显示旧单元格)。 But it seems , If I make custom its not retaining. 但似乎,如果我自定义其不保留。 If I keep
UITableViewCellSelectionStyleNone
, cell.backgroundColor
it works. 如果我保持
UITableViewCellSelectionStyleNone
, cell.backgroundColor
它可以工作。 But not highlighting :( 但不突出显示:(
[[NSUserDefaults standardUserDefaults]setInteger:indexPath.row forKey:@"SSLastSelectedLeftMenuIndex"];
I am initializing UIView
(LeftMenu) with frame. 我正在用框架初始化
UIView
(LeftMenu)。
Issue : Gray Color on Highlight after few mins custom yellow highlight coming and Not retaining selected cell color. 问题:几分钟后高亮显示灰色,自定义黄色高亮显示,并且未保留选定的单元格颜色。
I know I am missing some silly thing. 我知道我缺少一些愚蠢的东西。 But its eating my time.
但是它消耗了我的时间。 Advance thanks :)
预先感谢:)
Update:- below is my -cellForRowAtIndexpath
method 更新:-下面是我的
-cellForRowAtIndexpath
方法
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if([[NSUserDefaults standardUserDefaults]integerForKey:@"SSLastSelectedLeftMenuIndex"]==indexPath.row)
{
///<HIGHLIGHT CODE>;
}
to select the last selectd cell .. (try) 选择最后选择的单元格..(尝试)
- (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
}
hop this helps u :) 希望这对你有帮助:)
if u are using UITableViewCell then u do something like this,for yellow color for selecting the cell 如果您使用的是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 the subclassed table cell there is one method, to display yellow color for selecting the 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];
}
}
You have misunderstood selection and highlighting a cell you should change your cellForRowAtIndexPath like below to select the previously selected item. 您对选择有误解,并突出显示一个单元格,应按如下所示更改cellForRowAtIndexPath来选择先前选择的项目。
before that Declare a globle variable _selctedIndex of type NSIndexpath to store the last selected cell 在此之前声明一个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;
}
After your tableView has reloaded call the below line of code 重新加载tableView之后,请调用以下代码行
[tableView selectRowAtIndexPath:_selctedIndex animated:NO scrollPosition:UITableViewScrollPositionTop];
}
The UITableViewCell class will assign the view you have mentioned to the property selectedBackgroundView instead of the Default blue color view for showing in the background when a cell is selected UITableViewCell类会将您提到的视图分配给selectedBackgroundView属性,而不是将默认的蓝色视图分配给选定单元格时在背景中显示的视图
I ran into the exact same problem in a project, when tapping on a cell long enough to engage highlight then moving to a scroll the UITableView holds onto the highlighted index somehow so that the next attempt to select uses that previous indexPath. 我碰到了一个项目中的完全相同的问题,当点击一个足够长的单元格以使其突出显示然后移动到滚动时,UITableView会以某种方式保留在突出显示的索引上,以便下次选择时使用该先前的indexPath。
To resolve I did the following 为了解决我做了以下事情
Add Property to Track Offending indexPath 添加属性以跟踪违规indexPath
...
@property (nonatomic,strong) NSIndexPath *cachedPath;
...
Track indexPath with didUnhighlightRowAtIndexPath 使用didUnhighlightRowAtIndexPath跟踪indexPath
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
self.cachedPath = indexPath;
}
On Drag reload offending indexPath 在拖动时重新加载有问题的indexPath
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if (self.cachedPath)
{
[self.tableView reloadRowsAtIndexPaths:@[self.cachedPath] withRowAnimation:UITableViewRowAnimationNone];
}
self.cachedPath = nil;
}
For good measure clear the property on selection 做好选择后,清除属性
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.cachedPath = nil;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.