English 中文(简体)
A. 从主要观点来看,从可转让的电文Cell中可转让的变量
原标题:Assigning variables from a UITableViewCell to be tweeted in main view controller

我设法重新调配了可转让的电文Cell的变数,并把它与TWTweetComposeViewController混为一谈,但我却遇到了一个问题。 它总是歪曲可调意见中最后一行的变量。

我在此阐述: 我有1艘tw子和4个UI子,位于可加利用的电文中。 4个职业介绍所正在从一个名单上删除信息,以填表。 每个囚室都有一个tw子,以敲响囚室的信息,但那是我处理问题的。 <>总是把表上最后一行提供的信息,而不是表中的斜线。 非常感谢任何帮助。

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

并且最后,主要观察控制器的tw:

- (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];
    }
}
最佳回答

您错误地认为,该囚室是在用户利用这一tw子时提供的。

You could, for example add a tag to the tweet-button that reflects the row index it is shown in.

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

一旦该纽吨启动,在相关行动方法范围内,你就只是要把 index子重新编成索引,并在字典内用它处理正确数据。

请注意,我给你留下任何范围检查和进一步的防御性方案拟订行动。

- (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];
    }
}
问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签