如何在iOS中管理自定义单元格标签值?

如何在iOS中管理自定义单元格标签值?,ios,objective-c,uitableview,ibaction,tableviewcell,Ios,Objective C,Uitableview,Ibaction,Tableviewcell,我正在使用故事板构建一个iOS应用程序。我使用了UITableViewController,它有6个自定义单元格,每个单元格包含三个IBOutlet按钮和一个IBOutlet标签 当用户单击某个特定自定义单元格中的任何按钮时,仅该特定单元格标签的值应更改 但实际情况是,每个自定义单元格中所有标签的值都会更改 这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexP

我正在使用故事板构建一个iOS应用程序。我使用了UITableViewController,它有6个自定义单元格,每个单元格包含三个IBOutlet按钮和一个IBOutlet标签

当用户单击某个特定自定义单元格中的任何按钮时,仅该特定单元格标签的值应更改

但实际情况是,每个自定义单元格中所有标签的值都会更改

这是我的密码:

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

    static NSString *Cellidentifier1 = @"List";
    Cell *cell1 = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
     cell1.selectionStyle = UITableViewCellSelectionStyleNone;
    // Configure the cell...

    long row = [indexPath row];

    cell1.First.tag=row;//button iboutlet in cell
    cell1.Second.tag=row;//button iboutlet in cell
    cell1.Third.tag=row;//button iboutlet in cell

    cell1.Level.tag=row;

    cell1.Level.text=Skill;//label iboutlet in cell

    return cell1;
 }


-(IBAction)FirstAction:(id)sender{

     Skill=@"first";
     [self.tableView reloadData];

}

-(IBAction)SecondAction:(id)sender{

     Skill=@"Second";
     [self.tableView reloadData];

}


-(IBAction)ThirdAction:(id)sender{

     Skill=@"Third";
     [self.tableView reloadData];

}

首先要做的是,选择行时使用委托方法
selectRowAtIndexPath
,然后可以使用indexath selected行按钮

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){

} 
选择行后,将执行所需的行按钮功能

请尝试下面的代码

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

    static NSString *Cellidentifier1 = @"List";
    Cell *cell1 = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
     cell1.selectionStyle = UITableViewCellSelectionStyleNone;
    // Configure the cell...

    long row = [indexPath row];

    cell1.First.tag=row;//button iboutlet in cell
    cell1.Second.tag=row;//button iboutlet in cell
    cell1.Third.tag=row;//button iboutlet in cell

    cell1.Level.tag=row;

    cell1.Level.text=Skill;//label iboutlet in cell
[cell1.First addTarget:self action:@selector(FirstAction:) forControlEvents:UIControlEventTouchUpInside];
[cell1.Second addTarget:self action:@selector(SecondAction:) forControlEvents:UIControlEventTouchUpInside];
[cell1.Third addTarget:self action:@selector(ThirdAction:) forControlEvents:UIControlEventTouchUpInside];

    return cell1;
 }

有几种方法可以做到这一点。但是,通过查看您的代码,我可以确保您从所选的单元格中获得正确的indexPath。假设已经设置了目标操作,您可以重写iAction方法,如下所示:

-(IBAction)firstAction:(UIButton *)sender{
    int row = sender.tag;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:row inSection:0];
    Cell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    Skill = @"first";
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
这将得到正确的单元格和索引路径。仅更新该单元格的标签,然后重新加载它。

有几个问题:

  • 您应该有一个反映表视图内容的模型。具体来说,现在您有一个
    技能
    值,但有六行。您希望维护的模型是一个值数组,类似于:

    @property (nonatomic, strong) NSMutableArray *values;
    

    注意,不需要标签号

  • 当您点击按钮时,您必须(a)确定表中对应的行;(b) 更新模型中的该行;和(c)重新加载表中的单行(将在模型中查找值):

    注意,
    发送者
    是按钮。因此,我通过抓取
    superview
    (单元格的内容视图)然后抓取其
    superview
    (单元格本身)来获取表视图单元格。如果您的视图层次结构不同,则根据需要进行更改,但希望这能说明这一点


  • 在IBOutlet方法中,添加此项以查找行的indexPath

    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
    
    您只想在指定的
    indepath

    要仅重新加载一个单元格,请执行此操作,而不是执行[self.tableView reloadData]


    [self.tableView重新加载rowsatindexpaths:@[hitIndex]和rowAnimation:UITableViewRowAnimationNone]

    单击第一个操作时,您正在设置技能,然后重新加载表格并将每个单元格设置为具有技能值。你需要一系列技能。@MikeTaverne谢谢你的回复这不是我想做的,所以在单元格中有一些按钮是通过点击标签的单元格值来改变的,但是现在它在所有单元格中都在改变,但是我想当用户点击某个特定单元格中的按钮时,那个特定单元格的值标签就改变了。我猜在某个时候,除了显示技能数据之外,你还会想对技能数据做些什么。如果您只关注显示问题,那么当您实际尝试使用从用户那里收集的信息时,您会感到失望。为了避免这种失望,需要考虑如何正确使用表视图。我的回答可能会有所帮助:谢谢回复@Alex int row=sender.tag;这里给出的错误属性是“tag”在类型为“\uu strong id”的对象上找不到。您必须将方法签名中的id更改为UIButton*,即-(iAction)firstAction:(UIButton*)sender,因为id没有标记,所以不要只更新单元格上的标签。更新支持tableview的模型(这意味着更改模型以便跟踪表中每一行的状态),然后重新加载单元格。这是至关重要的。如果只更新单元格标签,如果单元格滚动出视图并被重新用于表格的另一行,你失去了用户所做的。谢谢@Dipel我应用了你的代码,但它给出了相同的结果。你好@Rob你能帮我增加在特定单元格中单击的按钮的大小,并尝试在单击按钮时更新按钮的CGRect,但不起作用。答案有点复杂(这取决于您的iOS版本、是否使用自动布局、按钮是否配置为将其框架转换为约束等)。最好在新的堆栈溢出问题中布局所有细节。
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *Cellidentifier1 = @"List";
        Cell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
    
        NSString *value = self.values[indexPath.row];
    
        cell.level.text = value;
    
        return cell;
    }
    
    - (void)updateCell:(UIView *)cell withValue:(NSString *)value {
        NSParameterAssert(cell);
    
        NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)cell];
        if (cell) {
            self.values[indexPath.row] = value;
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
    
    - (IBAction)didTapFirstButton:(id)sender {
        [self updateCell:[[sender superview] superview] withValue:@"first"];
    }
    
    - (IBAction)didTapSecondButton:(id)sender{
        [self updateCell:[[sender superview] superview] withValue:@"second"];
    }
    
    - (IBAction)didTapThirdButton:(id)sender{
        [self updateCell:[[sender superview] superview] withValue:@"third"];
    }
    
    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];