从UITableViewCell分配变量以在主视图控制器中发布

时间:2012-01-14 21:17:36

标签: ios variables twitter uitableview

我设法重新分配UITableViewCell的变量并使用TWTweetComposeViewController将其发送到推文,但我遇到了一个问题。它始终在UITableView的最后一行发布变量。

这是我的设置:我在UITableViewCell中有1个推文按钮和4个UILabel。 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];
    }
}

1 个答案:

答案 0 :(得分:1)

您错误地认为在用户点击该推文按钮的同时呈现/创建了该单元格。

例如,您可以在tweet按钮上添加一个标记,以反映其显示的行索引。

// 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];
    }
}