本文介绍了实现一个赞按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Parse作为后端,我无法在表格单元格中实现类似按钮的工作方式.表格单元格中有一个按钮,使用发件人/标签进行调用.这是代码.
I'm having trouble implementing a working like button in a table cell, using Parse as the backend. There is a button in the tablecell, which is called using a sender/tag. Here's the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FeedCell";
FeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
PFObject *post = [postArray objectAtIndex:indexPath.row];
cell.likeForYa.tag = indexPath.row;
[cell.likeForYa addTarget:self
action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
}
在发送方空白中,代码如下:
In the sender void, here's the code:
-(void)aMethod:(id)sender {
UIButton *senderButton = (UIButton *)sender;
NSLog(@"current Row=%d",senderButton.tag);
PFObject *tempObject = [postArray objectAtIndex:senderButton.tag];
NSLog(@"%@", tempObject.objectId);
//add the object ID for the cell we are liking to the array of liked items in the user class in parse
[[PFUser currentUser]addUniqueObject:tempObject.objectId forKey:@"liked"];
[[PFUser currentUser] saveInBackground];
PFObject* like = [PFObject objectWithClassName:@"Like"];
[like setObject:[PFUser currentUser][@"username"] forKey:@"username"];
[like setObject:tempObject.objectId forKey:@"photo"];
[like saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
PFQuery *query = [PFQuery queryWithClassName:@"Like"];
[query whereKey:@"photo" equalTo:tempObject.objectId];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(@"Number: %lu", (unsigned long)objects.count);
//cell.lik.text = [NSString stringWithFormat:@"%lu",(unsigned long)objects.count];
}];
}];
}
单击该按钮时,不存储任何内容,objects.count的日志返回0.有什么想法吗?
When the button is clicked, nothing is stores and the log for objects.count returns 0. Any ideas?
推荐答案
所以在这里,您可以使用PFQueryTableViewController进行子类化,并且tableViewCell可能看起来像这样:
So here you subclass with PFQueryTableViewController and your tableViewCell may look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"FeedCell";
FeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *likeForYa = [UIButton buttonWithType:UIButtonTypeCustom];
[likeForYa setTag:CellLikeForYaTag];
[cell.contentView addSubview:likeForYa];
[likeForYa addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
}
UIButton * likeForYa = (UIButton*) [cell.contentView viewWithTag:CellLikeForYaTag];
// check if current user like the post and change like-button image accordingly
if ([[object objectForKey:@"whoLiked"]containsObject:[PFUser currentUser].objectId]) {
[likeForYa setImage:[UIImage imageNamed:@"pressedLike.png"] forState:UIControlStateNormal];
} else {
[likeForYa setImage:[UIImage imageNamed:@"unpressedLike.png"] forState:UIControlStateNormal];
}
}
这是aMethod:
- (void)aMethod:(UIButton *)button{
CGPoint hitPoint = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
PFObject *object = [self.objects objectAtIndex:hitIndex.row];
// check if current user already liked the post
if (![[object objectForKey:@"whoLiked"]containsObject:[PFUser currentUser].objectId]) {
//add the object ID for the cell we are liking to the array of liked items in the user class in parse
[[PFUser currentUser] addUniqueObject:object.objectId forKey:@"liked"];
[[PFUser currentUser] saveInBackground];
//add the user ID to the post that the user liked
[object addUniqueObject:[PFUser currentUser].objectId forKey:@"whoLiked"];
[object saveInBackground];
} else {
//remove the object ID for the cell we are liking to the array of liked items in the user class in parse
[[PFUser currentUser] removeObject:object.objectId forKey:@"liked"];
[[PFUser currentUser] saveInBackground];
//remove the user ID to the post that the user liked
[object removeObject:[PFUser currentUser].objectId forKey:@"whoLiked"];
[object saveInBackground];
}
[self.tableView reloadData];
}
这篇关于实现一个赞按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!