0

我有以下程序结构:

-(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

      }

我需要的是,在运行时标签更改后,按钮也应该执行相应的操作。

例如,我正在关注某人 [按钮标签取消关注],如果我点击按钮 [按钮标签在那一刻变为关注(这完全可以发生)。现在,当我再次单击带有“关注”标签的相同按钮时,它会引发异常。

如何去做。请帮我弄清楚?

4

2 回答 2

0

我是 iOS 新手,但是:

  1. 设置一个有按钮状态的属性,或者扩展按钮(有些人不推荐)
  2. 当它改变状态时,改变颜色和标签。

那不就行了。可能其他用户有更好的解决方案。

干杯。

于 2012-05-17T18:43:11.327 回答
0

编辑:解决了。我之前添加了以下代码行

[发件人 setTitle:@"Follow" forState:UIControlStateNormal];

 [sender addTarget:self action:@selector(buttonClicked2:) forControlEvents:UIControlEventTouchUpInside];

cause of exception was i added colon (:) twice after @selector(buttonClicked2) whereas i need only one colon.

我希望这个问题也可以作为其他用户的参考。

于 2012-05-19T07:58:50.743 回答