ios - deselectrowatindexpath 文本突出显示消失了

标签 ios objective-c uitableview ios7

我正在将 UITableViewCelldetailTextLabel 设置为 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

输出如下

enter image description here

编辑您的代码

  - (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/

相关文章:

ios - 如何获取某个 Realm 对象在 Results 中的索引?

ios - 如何使用 ios sdk 在没有权限对话框的情况下登录 facebook?

ios - Swift:不同的构建连接到不同的服务器

ios - 如何检测 iPad 上的智能/外接键盘?

ios - 根据另一个数组的顺序对数组进行高效排序

ios/objective-c : Testing for existence of NSUserDefault

ios - 自定义 UITableViewCells 和 UITableView 的问题

ios - 子类化 UIButton 并检测 UIControlEventTouchUpInside

objective-c - NSUInteger 和 NSInteger 桥接到 Swift

ios - SwiftUI 中带有 UIViewRepresentable 的 UITableView