私は目的の c に不慣れで、目的指向のアイデア全体を理解しようとしています。私のアプリには、すべてのページで使用されるチェック マーク (like) ボタンがあるため、すべてのビューにチェック マーク メソッドを配置する代わりに、それらを独自のクラスに移動して呼び出したいと考えています。どうすればいいですか?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
...
//create check mark button
UIButton *checkButton = [[UIButton alloc] initWithFrame:CGRectMake(270, 0, 50, 90)];
//user has already check marked post
if ([checkActivityArray containsObject:[object objectId]]){
[checkButton setTitleColor: [UIColor blueColor] forState:UIControlStateNormal];
[checkButton addTarget:self action:@selector(minusCheckMark:) forControlEvents:UIControlEventTouchUpInside];
}
//user has NOT already check marked post
else{
[checkButton setTitleColor: [UIColor grayColor] forState:UIControlStateNormal];
[checkButton addTarget:self action:@selector(addCheckMark:) forControlEvents:UIControlEventTouchUpInside];
}
[checkButton setTitle:@"CH" forState:UIControlStateNormal];
[checkButton setTag:indexPath.row];
[cell.contentView addSubview:checkButton];
...
return cell;
}
- (void)addCheckMark:(UIButton*)button{
[button setTitleColor: [UIColor blueColor] forState:UIControlStateNormal];
PFObject *place = [placesArray objectAtIndex:[button tag]];
PFObject *checkActivity = [PFObject objectWithClassName:@"activity"];
[checkActivity setObject:place forKey:@"place"];
[checkActivity setObject:@"checkMark" forKey:@"type"];
[checkActivity setObject:[PFUser currentUser] forKey:@"fromUser"];
[checkActivity setObject:[place objectForKey:@"user"] forKey:@"toUser"];
[checkActivity saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error){
NSLog(@"Check marked post");
//add post to checkActivity array
[checkActivityArray addObject:[place objectId]];
[checkActivityArray writeToFile:checkMarkArrayFileName atomically:YES];
//increase check mark count
PFObject *checkCount = [placesArray objectAtIndex:[button tag]];
[checkCount incrementKey:@"checkMarkCount"];
[checkCount saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
[self.tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject:[NSIndexPath indexPathForRow: [button tag] inSection:0]] withRowAnimation:NO];
}];
}
else
NSLog(@"%@", error);
}];
}
- (void)minusCheckMark:(UIButton*)button{
[button setTitleColor: [UIColor grayColor] forState:UIControlStateNormal];
PFObject *place = [placesArray objectAtIndex:[button tag]];
PFQuery *queryCheckActivity = [PFQuery queryWithClassName:@"activity"];
[queryCheckActivity whereKey:@"place" equalTo:place];
[queryCheckActivity whereKey:@"type" equalTo:@"checkMark"];
[queryCheckActivity whereKey:@"fromUser" equalTo:[PFUser currentUser]];
[queryCheckActivity findObjectsInBackgroundWithBlock:^(NSArray *activities, NSError *error) {
if (!error) {
for (PFObject *activity in activities) {
[activity delete];
}
//pemove post from checkActivity array
[checkActivityArray removeObject:[place objectId]];
[checkActivityArray writeToFile:checkMarkArrayFileName atomically:YES];
//decrease check mark count
PFObject *checkCount = [placesArray objectAtIndex:[button tag]];
[checkCount incrementKey:@"checkMarkCount" byAmount:[NSNumber numberWithInt:-1]];
[checkCount saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
[self.tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject:[NSIndexPath indexPathForRow: [button tag] inSection:0]] withRowAnimation:NO];
}];
}
else
NSLog(@"%@", error);
}];
}