gpt4 book ai didi

ios - 使用标签没有得到正确的索引?

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

我有一个应用程序,您可以在其中向帖子添加评论。我正在使用 [sender tag] 来获取索引,但它总是返回相同的帖子。所以无论我点击哪个帖子单元格,它总是将它添加到同一个单元格而不是我点击的单元格。

非常感谢任何帮助。

这是我的代码(请注意,我已将代码剥离到我认为重要的部分,以便更容易阅读,因为某些函数有很多代码。如果您需要查看更多代码,请告诉我):

在每个单元格上设置评论按钮:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

[cell.commentButton addTarget:self action:@selector(commentButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];

return cell;

}

评论按钮。只是执行转场:

- (void)commentButtonClick:(id)sender {

[self performSegueWithIdentifier:@"addCommentSegue" sender:sender];

}

准备segue(我将它们发送到带有文本字段和保存按钮的基本 View Controller ):

else if ([segue.identifier isEqualToString:@"addCommentSegue"]) {

GFAddCommentViewController *secondDestViewController = [[segue destinationViewController] topViewController];

NSInteger index = [sender tag];

NSDictionary *rootObject = self.posts[index];
NSDictionary *post = rootObject[@"post"];
NSDictionary *group = post[@"group"];

secondDestViewController.postId = [post[@"id"] copy];
secondDestViewController.groupName = [group[@"name"] copy];
secondDestViewController.postBody =[post[@"body"] copy];
}

当他们在新 View Controller 上单击发送时,这是功能:

-(void)addComment:(id)sender {

GFCredentialStore *credentialStore = [[GFCredentialStore alloc] init];

NSString * authToken = [credentialStore authToken];

NSString * addCommentURL = [NSString stringWithFormat:@"%s%s/%@/%s", kBaseURL, kPostURL, self.postId, kCommentURL];

NSString * commentBody = self.commentTextField.text;

NSMutableDictionary *mutableParams = [NSMutableDictionary dictionary];

if (commentBody) {
[mutableParams setObject:commentBody forKey:@"comment[body]"];
}

[SVProgressHUD showWithStatus:@"Adding Comment"];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:authToken forHTTPHeaderField:@"auth_token"];
[manager POST:addCommentURL parameters:mutableParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
[SVProgressHUD showSuccessWithStatus:@"Comment Added"];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

}

只是为了澄清它已成功向数据库添加评论,只是 post.id 不正确。

最佳答案

你确定你设置的按钮标签正确吗?看来你应该这样设置

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

[cell.commentButton addTarget:self
action:@selector(commentButtonClick:)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
cell.commentButton.tag = indexPath.row;

return cell;

}

关于ios - 使用标签没有得到正确的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23017084/

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