【问题标题】:Change button's title on selecting table view cell在选择表格视图单元格时更改按钮的标题
【发布时间】:2013-05-13 14:59:23
【问题描述】:

我有一些按钮说明:“点击填写...”。当点击它们时,会弹出一个带有选项的表格视图。如何将这些选项链接到按钮,以便“点击填写..”被用户选择的数据/行替换?我不知道从哪里开始,提前谢谢。

【问题讨论】:

    标签: ios uitableview uibutton


    【解决方案1】:

    试试这个:

    - (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
         NSUInteger row = [indexPath row];
         NSString *newText = [youArray objectAtIndex:row]; //yourArray contains your datas (of type NSString in this example)
    
         //consider your button btn is an inst var
         [btn setTitle:newText forState:UIControlStateNormal];
    }
    

    【讨论】:

    • 您好,感谢您的回复。我按照你的建议做了,但我的按钮似乎没有更新该字段。你能更具体一点吗?提前致谢。
    • 我的数组:NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil]; self.tableViewArray = 数组;
    • tableview 代码:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [tableViewArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableIdentifier"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"SimpleTableIdentifier"]; } cell.textLabel.text = [tableViewArray objectAtIndex:indexPath.row];返回单元格; }
    • 在我的代码中,将youArray改成tableViewArray,btn改成要更新的按钮对应的ivar的名字。
    • [_industry setTitle: @"myTitle" forState: UIControlStateNormal];将按钮的标题更改为 myTitle。但是,当我尝试使用您的代码通过在表格视图中选择一行来动态更改标题时,它只是不会更新..
    【解决方案2】:

    //在viewcontroller.h文件中创建如下属性,点击按钮弹出

    @property (strong, nonatomic) UIButton  *button;
    

    //点击按钮时调用弹出视图如下

    YourTableViewController *tables = [UITableviewController alloc] init];
    tables.button = buttonObjectWhoesTextYouWantToChange;
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:tables];
    popover.delegate = self;
    popover.popoverContentSize=CGSizeMake(280.0, 327.0);
    

    // 现在在tableviewcontroller的didSelectRowAtIndexPath方法中加入这一行

    self.button.text = cell.textLabel.text;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2019-10-24
      • 2011-04-27
      • 1970-01-01
      • 2017-02-03
      • 2015-08-10
      相关资源
      最近更新 更多