すべてのセルに UIButton があるアプリケーションを実装しています。このボタンは特定のイベントをユーザーのお気に入りに追加し、その画像をもう一度クリックすると、そのイベントがユーザーのお気に入りから削除されます。どのセルのユーザーがそのボタンを押したかを検出し、それに応じて背景画像を変更したい。
これが私のコードです(私が試したものです。)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
uncheckButton = [[UIButton alloc]init];
appDelegate.setValue = 0;
[uncheckButton setFrame:CGRectMake (3, 25, 30, 30)];
[uncheckButton setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[uncheckButton setSelected:NO];
[uncheckButton addTarget:self action:@selector(uncheckedButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:uncheckButton];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
-(IBAction)uncheckedButtonClicked:(id)sender
{
if(appDelegate.setValue ==0)
{
appDelegate.setValue =1;
[uncheckButton addTarget:self action:@selector(uncheckedButtonClicked:) forControlEvents: UIControlEventTouchUpInside];
[uncheckButton setFrame:CGRectMake(3, 25, 30, 30)];
[uncheckButton setImage:[UIImage imageNamed:@"checked_red.png"] forState:UIControlStateNormal];
Reachability *reachability = [Reachability reachabilityForInternetConnection] ;
NetworkStatus netWorkStatus = [reachability currentReachabilityStatus] ;
if (netWorkStatus == ReachableViaWWAN || netWorkStatus == ReachableViaWiFi)
{
[NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
NSLog(@"%@",appDelegate.user_id);
Communication *objCommunication = [[Communication alloc] init];
objCommunication.delegate = self;
[objCommunication setServiceFor:@"My_Agenda_add"];
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"%@/api/join_session",BASE_URL]];
NSString *body = [ NSString stringWithFormat:@"user_id=%@&session_id=%@",[appDelegate.user_id objectAtIndex:0],[[self.globalSessionArray valueForKey:@"_id"]valueForKey:@"$id"]];
[objCommunication makeAsynchronousRequestWithUrl:url withBodyString:body andWithMethod:@"POST"];objCommunication = nil;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:app_name message:network_connectivity delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
else if(appDelegate.setValue)
{
appDelegate.setValue = 0;
[uncheckButton addTarget:self action:@selector(uncheckedButtonClicked:) forControlEvents: UIControlEventTouchUpInside];
[uncheckButton setFrame:CGRectMake(3, 25, 30, 30)];
[uncheckButton setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
Reachability *reachability = [Reachability reachabilityForInternetConnection] ;
NetworkStatus netWorkStatus = [reachability currentReachabilityStatus] ;
if (netWorkStatus == ReachableViaWWAN || netWorkStatus == ReachableViaWiFi)
{
[NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
NSLog(@"%@",appDelegate.user_id);
Communication *objCommunication = [[Communication alloc] init];
objCommunication.delegate = self;
[objCommunication setServiceFor:@"My_Agenda_remove"];
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"%@/api/unjoin_session",BASE_URL]];
NSString *body = [ NSString stringWithFormat:@"user_id=%@&session_id=%@",[appDelegate.user_id objectAtIndex:0],[[self.globalSessionArray valueForKey:@"_id"]valueForKey:@"$id"]];
[objCommunication makeAsynchronousRequestWithUrl:url withBodyString:body andWithMethod:@"POST"];objCommunication = nil;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:app_name message:network_connectivity delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0] ;
NSLog(@"%@",indexPath);
UITableViewCell* cell = [self.agendaTableView cellForRowAtIndexPath:indexPath];
[cell addSubview:uncheckButton];
[self.agendaTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}