Ios 分配UITableViewCell中的变量以在主视图控制器中发送tweet
我设法重新分配UITableViewCell的变量,并使用TWTweetComposeViewController将其发送到tweet,但我遇到了一个问题。它总是从UITableView的最后一行推送变量 这是我的设置:我在一个UITableViewCell中有1个推特按钮和4个UILabel。4个UILabel从Plist中提取信息以填充表。每个单元格中都有一个tweet按钮来推送单元格信息,但这正是我遇到问题的地方它总是从表中的最后一行而不是它所在的行中推送信息。非常感谢您的帮助 UITableViewCell.hIos 分配UITableViewCell中的变量以在主视图控制器中发送tweet,ios,variables,twitter,uitableview,Ios,Variables,Twitter,Uitableview,我设法重新分配UITableViewCell的变量,并使用TWTweetComposeViewController将其发送到tweet,但我遇到了一个问题。它总是从UITableView的最后一行推送变量 这是我的设置:我在一个UITableViewCell中有1个推特按钮和4个UILabel。4个UILabel从Plist中提取信息以填充表。每个单元格中都有一个tweet按钮来推送单元格信息,但这正是我遇到问题的地方它总是从表中的最后一行而不是它所在的行中推送信息。非常感谢您的帮助 UITab
@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;
}
最后,主视图控制器中的tweet设置:
- (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];
}
}
您错误地假设单元格是在用户点击该tweet按钮的同时呈现/创建的 例如,您可以向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;
}
一旦该按钮被激活,在connected action方法中,您只需从标记中获取该索引,并使用它来寻址字典中的正确数据
请注意,我留下了所有的范围检查和进一步的防御编程动作供您完成
- (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];
}
}
你应该在每个标签上添加标签,然后通过发送者参数通过标签访问它们。实际上,我在主控制器中设置了tweet按钮,将其链接到发送tweet的“内部润色”操作。我将试用你的代码,看看它是否有效。谢谢。在你的问题中,你告诉我们每个单元格都包含一个tweet按钮。要么是我误解了你,要么是你的评论毫无意义。也许我应该更明确一点。我正在使用一个带有动态原型单元的故事板。4个标签和tweet按钮位于一个原型单元内。我在一个单独的类中声明标签,并在主控制器中声明按钮。我可以用这个设置发tweet,所以我必须用你的代码做一些实验。