我正在将 UITableViewCell
的 detailTextLabel
设置为 highlighted:YES;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.detailTextLabel setHighlighted:YES];
}
但是出于什么原因 deselectRowAtIndexPath:indexPath
破坏了我的 detailTextLabel
的突出显示?!
更新:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row != 0)
{
if(CellIsSelected)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:selectedIndexPath];
[cell.detailTextLabel setHighlighted:NO];
}
[self.tableView beginUpdates];
if ([self datePickerIsShown] && (self.datePickerIndexPath.row - 1 == indexPath.row)){
[self hideExistingPicker];
}else {
NSIndexPath *newPickerIndexPath = [self calculateIndexPathForNewPicker:indexPath];
if ([self datePickerIsShown]){
[self hideExistingPicker];
selectedIndexPath = indexPath;
if (newPickerIndexPath.row == 2)
selectedIndexPath = [NSIndexPath indexPathForRow:2 inSection:indexPath.section];
else if (newPickerIndexPath.row == 3)
selectedIndexPath = [NSIndexPath indexPathForRow:1 inSection:indexPath.section];
}
else
selectedIndexPath = indexPath;
[self showNewPickerAtIndex:newPickerIndexPath];
self.datePickerIndexPath = [NSIndexPath indexPathForRow:newPickerIndexPath.row + 1 inSection:0];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView endUpdates];
CellIsSelected = YES;
[picker reloadAllComponents];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kOtherCellID];
cell.textLabel.text = text;
if(CellIsSelected)
[cell.detailTextLabel setHighlighted:YES];
else
[cell.detailTextLabel setHighlighted:NO];
return cell;
}
最佳答案
以下是满足您要求的一种方法 我正在发布示例代码,在新项目中尝试一下,尝试一下希望这对您有帮助
在ViewController.h
文件中
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)NSIndexPath *selectedIndexPath;
@end
在ViewController.m
文件中
#import "ViewController.h"
@interface ViewController ()
{
BOOL isCellSelected;
}
@end
@implementation ViewController
@synthesize selectedIndexPath = _selectedIndexPath;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
isCellSelected = NO;
_selectedIndexPath = [[NSIndexPath alloc]init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CELL"];
}
cell.textLabel.text = @"Test";
cell.detailTextLabel.text = @"EXAMPLE";
cell.detailTextLabel.highlightedTextColor = [UIColor greenColor]; //check if higlight is remains same or not
if(isCellSelected)
[cell.detailTextLabel setHighlighted:YES];
else
[cell.detailTextLabel setHighlighted:NO];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(isCellSelected)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:_selectedIndexPath];
[cell.detailTextLabel setHighlighted:NO];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES]; //added
isCellSelected = YES;
_selectedIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:_selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end
输出如下
编辑您的代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row != 0)
{
if(CellIsSelected)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:selectedIndexPath];
[cell.detailTextLabel setHighlighted:NO];
}
[self.tableView beginUpdates];
if ([self datePickerIsShown] && (self.datePickerIndexPath.row - 1 == indexPath.row))
{
[self hideExistingPicker];
}else {
NSIndexPath *newPickerIndexPath = [self calculateIndexPathForNewPicker:indexPath];
if ([self datePickerIsShown])
{
[self hideExistingPicker];
selectedIndexPath = indexPath;
if (newPickerIndexPath.row == 2)
selectedIndexPath = [NSIndexPath indexPathForRow:2 inSection:indexPath.section];
else if (newPickerIndexPath.row == 3)
selectedIndexPath = [NSIndexPath indexPathForRow:1 inSection:indexPath.section];
}
else
selectedIndexPath = indexPath;
[self showNewPickerAtIndex:newPickerIndexPath];
self.datePickerIndexPath = [NSIndexPath indexPathForRow:newPickerIndexPath.row + 1 inSection:0];
}
[self.tableView endUpdates];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
CellIsSelected = YES;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[picker reloadAllComponents];
}
}
关于ios - deselectrowatindexpath 文本突出显示消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23678424/