我正在实现一个应用程序,其中每个单元格上都有 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];
}