我在tableView中选择一行时遇到问题,问题是当我第一次选择一行时,它会以灰色突出显示但没有任何反应,我必须再次选择一行来执行“didSelectRowAtIndexPath” “并带我去另一个ViewController
// Number of rows in the tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 6;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 175.0;
}
// Populating each cell at a time.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
ClientCustomCell *cell = (ClientCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"ClientCustomCell" owner:self options:nil];
cell = self.clientCustomCell;
self.clientCustomCell = nil;
cell.name = [nameArray objectAtIndex:indexPath.row];
cell.number = [numberArray objectAtIndex:indexPath.row];
cell.postal = [postalArray objectAtIndex:indexPath.row];
cell.locality = [localityArray objectAtIndex:indexPath.row];
}
[cell setSelectionStyle:(UITableViewCellSelectionStyleBlue)];
return cell;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"row selecter %d", (int)indexPath.row);
InfoViewController *info = [[InfoViewController alloc] init];
[self.navigationController pushViewController:info animated:YES];
}
答案 0 :(得分:5)
您不小心实施了 DESELECT rowAtIndexPAth,而不是 SELECT rowAtIndexPath。
答案 1 :(得分:4)
didDeselectRowAtIndexPath
方法保留在didSelectRowAtIndexPath
:的位置
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
答案 2 :(得分:1)
使用以下代码更新您的代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"row selecter %d", (int)indexPath.row);
InfoViewController *info = [[InfoViewController alloc] init];
[self.navigationController pushViewController:info animated:YES];
}
答案 3 :(得分:0)
您已实施didDeselectRowAtIndexPath
,这应为didSelectRowAtIndexPath
:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
答案 4 :(得分:0)
tableView iOS 上的选择行
对于目标 c
-(void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
适用于 Swift 4-5
import UIKit
extension UITableView {
func deselectSelectedRow(animated: Bool)
{
if let indexPathForSelectedRow = self.indexPathForSelectedRow {
self.deselectRow(at: indexPathForSelectedRow, animated: animated)
}
}
}