【问题标题】:Passing name of cell to another view via table cell button通过表格单元格按钮将单元格名称传递到另一个视图
【发布时间】:2012-12-03 02:48:05
【问题描述】:

好的,所以我想将一个视图控制器中表格单元格中包含的名称传递给另一个视图控制器中的数组。我想通过也包含在第一个表格的单元格中的按钮来执行此操作。

这里是表格单元格的描述:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// add "add" button
UIButton *addFriendButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
addFriendButton.frame = CGRectMake(250.0f, 5.0f, 75.0f, 30.0f);

[cell addSubview:addFriendButton];

[addFriendButton addTarget:self
                    action:@selector(addFriend:)
                    forControlEvents:UIControlEventTouchUpInside];

if(!cell)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;

}

这里是按钮方法的描述:

- (IBAction)addFriend:(id)sender
{
    NSLog(@"Add friend.");



}

第一个视图控制器的名称 - 执行传递的控制器 - 称为 ViewController - 而第二个视图控制器 - 接收传递信息的控制器 - 称为 MyMealViewController。我希望将传递的信息存储在其中的数组称为 myMenuArray。

我认为这基本上是所有相关信息。如果您需要我提供任何其他信息 - 或者如果您需要澄清我提出的问题 - 让我的问题可以回答,请告诉我!

【问题讨论】:

  • 不清楚您的问题是什么。什么不工作?一个明显的错误是,在 dequed 单元格为 nil 的情况下,您在完成单元格配置后分配新单元格。您应该将单元分配向上移动到出列队列的正下方
  • 感谢您指出错误,我按照您的建议进行了更改。至于我的问题:并不是说任何东西都“不起作用”,只是我需要一些指导来弄清楚如何在第二个代码块中实现“addFriend”操作,一旦点击按钮就会传递名称。

标签: ios xcode uitableview button


【解决方案1】:

你可以使用

cell.textLabel.text = [array objectAtIndex:indexPath.row];

[[addFriendButton addTarget:self
                        action:@selector(addFriend:) toTarget:self      withObject:cell.textLabel.text]
forControlEvents:UIControlEventTouchUpInside];

- (void)addFriend:(NSString *)theFriendName 
{
   NSLog(@"Add friend.");
// Now here you make the instance of the new ViewController and you set the text you are     adding.
MyViewController *theVC = [MyViewController alloc] initWithNibName:@"MyViewController"     bundle:nil];
[theVC setNewFriend:theFriendName];
[self presentModalViewController:theVC animated:YES];

}

【讨论】:

    猜你喜欢
    • 2020-03-03
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    相关资源
    最近更新 更多