gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:57:52 24 4
gpt4 key购买 nike

我正在将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com