[英]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
,我正在捕獲最后選擇的索引(當我選擇新單元格時,應該取消突出顯示舊單元格)。 但似乎,如果我自定義其不保留。 如果我保持UITableViewCellSelectionStyleNone
, cell.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.row)
{
///<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.