2012-01-14 5 views
0

UITableViewCellの変数を再割り当てし、TWTweetComposeViewControllerでツイートするようになりましたが、問題が発生しました。 UITableViewの最後の行から変数をtweetingします。UITableViewCellの変数をメインビューコントローラでツイートする

ここに私の設定です:私は1つのツイートボタンと4つのUILabelsがUITableViewCellにあります。 4つのUILabelsはPlistから情報を引き出してテーブルを作成しています。すべてのセルにツイートボタンがあり、セル情報をつぶすことができますが、それが問題の原因です。 テーブル内の最後の行の情報を、その行の代わりに常につぶやきます。何か助けてください。

UITableViewCell.h

@property (nonatomic, strong) IBOutlet UILabel *playerOneLabel; 
@property (nonatomic, strong) IBOutlet UILabel *playerOneScoreLabel; 

@property (nonatomic, strong) IBOutlet UILabel *playerTwoLabel; 
@property (nonatomic, strong) IBOutlet UILabel *playerTwoScoreLabel; 

メインビューコントローラのセットアップ:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"ScoreListCell"; 

    ScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 

    NSDictionary * dictionary = [scoresArray objectAtIndex:indexPath.row]; 
    cell.playerOneLabel.text = [dictionary objectForKey:@"playerOneName"]; 
    cell.playerOneScoreLabel.text = [dictionary objectForKey:@"playerOneScore"]; 
    cell.playerTwoLabel.text = [dictionary objectForKey:@"playerTwoName"]; 
    cell.playerTwoScoreLabel.text = [dictionary objectForKey:@"playerTwoScore"]; 

    self.player1Name = cell.playerOneLabel; 
    self.player1Score = cell.playerOneScoreLabel;   
    self.player2Name = cell.playerTwoLabel; 
    self.player2Score = cell.playerTwoScoreLabel; 

    return cell; 
} 

、最終的には、メインビューコントローラでのつぶやきのセットアップ:

- (IBAction)twitter:(id)sender { 

    if ([TWTweetComposeViewController canSendTweet]) 
    { 
     TWTweetComposeViewController *tweetSheet = 
     [[TWTweetComposeViewController alloc] init]; 
     NSString *text = [NSString stringWithFormat:@"%@-%@, %@-%@", 
          player1Name.text, player1Score.text, player2Name.text, player2Score.text]; 
     [tweetSheet setInitialText:text]; 
     [self presentModalViewController:tweetSheet animated:YES]; 
    } 
    else 
    { 
     UIAlertView *alertView = [[UIAlertView alloc] 
            initWithTitle:@"Sorry"                
            message:@"Tweet unsuccessful. Make sure your device has an internet connection and you have a Twitter account."               
            delegate:self            
            cancelButtonTitle:@"OK"             
            otherButtonTitles:nil]; 
     [alertView show]; 
    } 
} 
+0

あなたが各ラベルにタグを追加してから送信者のパラメータ – cpjolicoeur

答えて

1

あなたは間違っていると仮定されていますセルは、ユーザがそのツイートボタンをタップするのと同時にレンダリング/作成される。

あなたは、例えば、それはに示されている行のインデックスを反映したつぶやきボタンにタグを追加することができます。

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ScoreListCell"; 

    ScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 

    //assign the row-index as a button tag - 
    //NOTE: this misses the way you fetch/create your tweetButton - 
    //  that would have to left to you as you missed to quote 
    //  that part in your sources 
    UIButton *tweetButton = ???; 

    tweetButton.tag = indexPath.row; 

    NSDictionary * dictionary = [scoresArray objectAtIndex:indexPath.row]; 
    cell.playerOneLabel.text = [dictionary objectForKey:@"playerOneName"]; 
    cell.playerOneScoreLabel.text = [dictionary objectForKey:@"playerOneScore"]; 
    cell.playerTwoLabel.text = [dictionary objectForKey:@"playerTwoName"]; 
    cell.playerTwoScoreLabel.text = [dictionary objectForKey:@"playerTwoScore"]; 

    return cell; 
} 

をそのボタンが接続されているアクションメソッド内で活性化されたら、あなたは、単にことをフェッチタグから戻ってインデックスを作成し、辞書内の正しいデータのアドレス指定に使用します。

私は、あなたが行うための範囲チェックと防御的なプログラミングアクションを残していたことに注意してください。

- (IBAction)twitter:(id)sender 
{ 
    UIButton *tweetButton = (UIButton *)sender; 
    unsigned int rowIndex = tweetButton.tag; 

    NSDictionary * dictionary = [scoresArray objectAtIndex:rowIndex]; 
    NSString *playerOneNameText = [dictionary objectForKey:@"playerOneName"]; 
    NSString *playerOneScoreText = [dictionary objectForKey:@"playerOneScore"]; 
    NSString *playerTwoNameText = [dictionary objectForKey:@"playerTwoName"]; 
    NSString *playerTwoScoreText = [dictionary objectForKey:@"playerTwoScore"]; 

    if ([TWTweetComposeViewController canSendTweet]) 
    { 
     TWTweetComposeViewController *tweetSheet = 
     [[TWTweetComposeViewController alloc] init]; 
     NSString *text = [NSString stringWithFormat:@"%@-%@, %@-%@", 
          playerOneNameText, playerOneScoreText, playerTwoNameText, playerTwoScoreText]; 
     [tweetSheet setInitialText:text]; 
     [self presentModalViewController:tweetSheet animated:YES]; 
    } 
    else 
    { 
     UIAlertView *alertView = [[UIAlertView alloc] 
            initWithTitle:@"Sorry"                
            message:@"Tweet unsuccessful. Make sure your device has an internet connection and you have a Twitter account."               
            delegate:self            
            cancelButtonTitle:@"OK"             
            otherButtonTitles:nil]; 
     [alertView show]; 
    } 
} 
+0

を通じてタグを介してそれらにアクセスする必要があり、私は実際には、送信の「タッチアップインサイド」アクションにリンク、メインコントローラで設定つぶやきボタンを持っていますつぶやく私はあなたのコードを試して、それが動作するかどうかを見ていきます。ありがとう。 –

+0

ご質問では、すべてのセルにつぶやきボタンが含まれているとお伝えしました。あなたが間違っているか、あなたのコメントが理にかなっていません。 – Till

+0

私はもっと明示されていたはずです。私は動的プロトタイプのセルでストーリーボードを使用しています。 4つのラベル&とツイートボタンはプロトタイプのセルの内側にあります。別のクラスのラベルとメインコントローラのボタンを宣言しています。私はこの設定を使ってつぶやくことができるので、あなたのコードを試してみる必要があります。 –

関連する問題