【问题标题】:Change specific button title in custom cell from known button tag从已知按钮标签更改自定义单元格中的特定按钮标题
【发布时间】:2014-08-09 08:45:56
【问题描述】:

我有一个带有 CustomCell 的 UITableView,每个单元格都有一个 UIButton,并且按钮在 cellForRowAtIndexPath 中获取标签

cell.ffButton.tag = indexPath.row;
[cell.ffButton addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];

如果用户按下UIButton,我希望它只更改按钮标题。

changeTitle 操作在我的 UITableView.m 中,而不是在 CustomCell.m 文件中

用谷歌搜索我需要的东西,但找不到任何东西。

有什么想法吗? :)

更新:

我的表格视图

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

    static NSString *CellIdentifier = @"NotMutualCustomCell";

    notMutualCustom *cell = (notMutualCustom *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.ffButton.hidden = NO;
    cell.ffButton.tag = indexPath.row;

    [cell.ffButton setTitle:@"Title" forState:UIControlStateNormal];
    [cell.ffButton addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

【问题讨论】:

    标签: ios objective-c xcode uitableview ios7


    【解决方案1】:

    您可以为按钮的不同状态指定不同的标题:

       [button setTitle:@"ON" forState:UIControlStateNormal];
       [button setTitle:@"OFF" forState:UIControlStateSelected];
    

    接下来,您可以在UIControlEventTouchUpInsideevent 的操作处理程序中更改按钮的状态:

     - (void)changeTitle:(UIButton *)button
     {
       button.state = button.state == UIControlStateNormal ? UIControlStateSelected : UIControlStateNormal;
       //keep new state of the button in model 
       self.dictionary[[@(button.tag) stringValue]] = @(state);
     }
    

    您必须将按钮的状态存储在应用程序的模型中。您可以为此目的使用字典(请记住正确初始化字典的值 - 到正确的状态)。

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     {
        //create cell 
        cell.button.state = [self.dictionary[[@(button.tag) stringValue]] integerValue];
        return cell;
     }
    

    【讨论】:

    • 我不想为不同的状态使用不同的标题,但是我尝试了您的代码并且它可以更改标题,但问题是当我从单元格滚动时然后滚动回它标题变回原来的标题。
    • 您必须在模型中保留状态的值。每次创建新单元格时,都必须使用此值来设置单元格中按钮的状态。我已经编辑了我的答案。
    • 我认为当前的答案解决了滚动时按钮状态不一致的问题。
    • 我不需要用按钮状态来改变标题
    【解决方案2】:

    自定义表格视图单元格

    [btnAddFriend setTitle:@"Follow" forState:UIControlStateNormal];
    [btnAddFriend setTitle:@"Unfollow" forState:UIControlStateSelected];
    

    表格视图实现

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
    if (!cell) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
        cell = [nibs firstObject];
        [cell.btnAddFriend addTarget:self action:@selector(followButtonTapped:forEvent:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return cell;
    }
    
    -(void)followButtonTapped:(id)sender forEvent:(UIEvent*)event{
    
    //
    UIButton *followButton = (UIButton*)sender;
    followButton.selected = !followButton.selected;
    
    //
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    
    // Lookup the index path of the cell whose checkbox was modified.
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
    
    NSLog(@"index:%@",indexPath); // you can get the index of array from here 
    
    }
    

    Reference TableViewCell Accessory

    【讨论】:

    • 那么我将如何更改按钮标题?
    • 当你点击按钮时,它的状态会改变,因为我们在自定义单元格中设置了选择状态(取消关注)和正常状态(btnAddFriend)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多