gpt4 book ai didi

ios - 我可以只对特定的单元格标识符执行 didSelectRowAtIndexPath 吗?

转载 作者:行者123 更新时间:2023-11-28 21:19:21 24 4
gpt4 key购买 nike

我在表格 View 中使用了两个不同的自定义表格单元格。也就是说,我只希望 didSelectRowAtIndexPath 在例如自定义单元标识符 2 被点击时触发(而不是在自定义单元标识符 1 被点击时触发)。我该怎么办?请参阅下面的代码。现在,didSelectRowAtIndexPath 会在任一单元格被点击时触发...

- (void)viewDidLoad {
[super viewDidLoad];


static NSString *ChatTableIdentifier = @"ChatTableViewCell";
static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";


UINib *nib = [UINib nibWithNibName: ChatTableIdentifier bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier: ChatTableIdentifier];

UINib *nib2 = [UINib nibWithNibName: ChatTableIdentifier2 bundle:nil];
[self.tableView registerNib:nib2 forCellReuseIdentifier: ChatTableIdentifier2];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *data = self.messages[indexPath.row];

id swaptime = data[@"swaptime"];
if ([swaptime isKindOfClass:[NSString class]]) {

static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";

SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2 forIndexPath:indexPath];

NSString *time = data[@"swaptime"];
cell.startTime.text = time;

NSString *timeEnd = data[@"endswaptime"];
cell.endTime.text = timeEnd;

return cell;
} else {
static NSString *ChatTableIdentifier = @"ChatTableViewCell";

ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];

NSString *userName = data[@"name"];
cell.sendingUser.text = userName;

NSString *messageBody = data[@"body"];
cell.messageDisplayed.text = messageBody;

NSString *timeReceived = data[@"published at"];
cell.timeStamp.text = timeReceived;

return cell;
}
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


SwapDetailsViewController *detailViewController = [[SwapDetailsViewController alloc]
initWithNibName:@"SwapDetailsViewController" bundle:nil];

detailViewController.swapDetails = [self.messages objectAtIndex:indexPath.row];

[self presentViewController:detailViewController animated:YES completion:nil];

}

最佳答案

您可以在 didSelectRowAtIndexPath 中放置一个 if 条件,就像您在 cellForRowAtIndexPath 中放置的那样

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSDictionary *data = self.messages[indexPath.row];

id swaptime = data[@"swaptime"];

//perform action based on this, and don't do anything in second case

if ([swaptime isKindOfClass:[NSString class]])

//this indexPath will always contain cell of one kind

}else{

// this indexPath will contain second cell kind
}
}

或者你也可以这样做-

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// this will give you cell at the selected indexPath
id cell = [tableView cellForRowAtIndexPath:indexPath]

//perform action based on this, and don't do anything in second case
if ([cell isKindOfClass:[SwapDetailTableViewCell class]])

}else{

}
}

关于ios - 我可以只对特定的单元格标识符执行 didSelectRowAtIndexPath 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40320188/

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