我有以下程序结构:
-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
...
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
...
...
if(condition)
{
do something;
}
else
{
if(condition)
{
unFollowButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
unFollowButton.frame = CGRectMake(180, 10, 120, 30);
unFollowButton.titleLabel.font = [UIFont systemFontOfSize:12];
unFollowButton.tag = indexPath.row;
unFollowButton.titleLabel.textColor = [UIColor blackColor];
[unFollowButton addTarget:self action:@selector(buttonClicked2:) forControlEvents:UIControlEventTouchUpInside];
NSLog(@"fCheckRowCheck Value in if Condition %@",fCheckRowCheck);
[unFollowButton setTitle:@"UnFollow" forState:UIControlStateNormal];
[cell.contentView addSubview:unFollowButton];
buttonValue = 0;
NSLog(@"buttonValue %d", buttonValue);
}
else
{
followButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
followButton.frame = CGRectMake(180, 10, 120, 30);
followButton.titleLabel.font = [UIFont systemFontOfSize:12];
followButton.tag = indexPath.row;
followButton.titleLabel.textColor = [UIColor blackColor];
[followButton setTitle:@"Follow" forState:UIControlStateNormal];
[followButton addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:followButton];
buttonValue = 1;
NSLog(@"buttonValue %d", buttonValue);
}
}
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.infos objectAtIndex:row];
return cell;
}
- (IBAction)buttonClicked2:(UIButton *)sender
{
NSLog(@"BUTTON_CLICKED");
NSIndexPath *indexPath = [folksFolksTable indexPathForCell:(UITableViewCell*) [[sender superview]superview]];
NSLog(@"[sender tag] is %d", [sender tag]);
....
....
....
//Set up URLConnection tp send information on button click
NSMutableString *postString = [NSMutableString stringWithString:kUnFollowURL];
[postString appendString: [NSString stringWithFormat:@"?%@=%@", kId, [user objectForKey:@"id"] ]];
[postString appendString: [NSString stringWithFormat:@"&%@=%@", kfId, [fId objectForKey:@"fID"] ]];
[postString setString: [postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"post string = %@", postString);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
followConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
NSLog(@"postconnection: %@", followConnection);
//Get Response from server
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: postString ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
NSLog(@"serverOutput = %@", serverOutput);
//Change the button lable from unfollow to follow
[sender setTitle:@"Follow" forState:UIControlStateNormal];
if([sender tag]==indexPath.row)
{
textField = (UITextField*)[cell viewWithTag:[sender tag]];
NSLog(@"txtF is %@",textField);
textField.hidden=NO;
}
}
- (IBAction)buttonClicked1:(UIButton *)sender
{
similar to buttonClicked 2
}
我需要的是,在运行时标签更改后,按钮也应该执行相应的操作。
例如,我正在关注某人 [按钮标签取消关注],如果我点击按钮 [按钮标签在那一刻变为关注(这完全可以发生)。现在,当我再次单击带有“关注”标签的相同按钮时,它会引发异常。
如何去做。请帮我弄清楚?