简体   繁体   English

如何更改放置在原型单元格中的UIButton标题?

[英]How to change UIButton title which placed in prototype cell?

I have TableView with custom type of cells. 我有自定义类型的单元格的TableView In prototype cell I have 3 buttons. 在原型cell我有3个按钮。 I want to be able change button title when I pressed on another. 我希望能够在按下另一个时更改按钮标题。 I trying this: 我试着这个:

- (IBAction) doSomething:(id) sender { 

    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.myTableView]; 
    NSIndexPath *hitIndex = [self.myTableView indexPathForRowAtPoint:hitPoint];

    NSLog(@"%ld", (long)hitIndex.row); //This works and I can see the row which button placed
    MyCustomViewCell *cell = [_myTableView dequeueReusableCellWithidentifier:@"myCell"];

    //I'm tried this:
    cell.button.titleLabel.text = @"111";

    //and I'm tried this:
    [cell.button setTitle:@"222" forState:UIControlStateNormal];

}

What I'm doing wrong? 我做错了什么?

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

     MyCustomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];

    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
        cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
    }

      cell.textLabel.text = _productNameForClearance;
      cell.imageView.image = _productImageForBasket;


    return cell;
}

Add action handler that provides touch event to button in cell. 添加动作处理程序,为单元格中的按钮提供触摸事件。 Like this: 像这样:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* reuseIdentifier = [self cellReuseIdentifierForIndexPath:indexPath];
    MyCustomCell* cell = (MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    [cell.button addTarget:self action:@selector(cellButtonAction:forEvent:) forControlEvents:UIControlEventTouchUpInside];
// your implementation

}

And here's cellButtonAction:forEvent: implementation: 这里是cellButtonAction:forEvent:实现:

- (IBAction)cellButtonAction:(id)sender forEvent:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [self.tableView indexPathForRowAtPoint:currentTouchPosition];

    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
    // your code here
    MyCustomCell* cell = [self.tableView cellForRowAtIndexPath: indexPath];
    [cell.button setTitle:@"222" forState:UIControlStateNormal];
}

Hope this helps. 希望这可以帮助。

Replace line: 替换线:

MyCustomViewCell *cell = [_myTableView dequeueReusableCellWithidentifier:@"myCell"];

with: 有:

MyCustomViewCell *cell = [_myTableView cellForRowAtIndexPath: hitIndex]

When you pressed the button the title is changed but when you scroll and the row with the changed title disappear the cell is recycled and when you scroll back the cell is taken from the recycled pool and is set up with initial values. 当您按下按钮时,标题会被更改,但是当您滚动并且带有更改标题的行消失时,单元格将被回收,当您向后滚动时,单元格将从循环池中获取并设置为初始值。 My advice is to create array where you keep track of your changed rows(titles) and in the cellForRowAtIndexPath: method you should displayed the changed title from that array. 我的建议是创建用于跟踪已更改的行(标题)的数组,并在cellForRowAtIndexPath:方法中显示该数组中已更改的标题。

The reason your code does not work is that this code gives you an entirely unrelated cell: 您的代码不起作用的原因是此代码为您提供了一个完全不相关的单元格:

MyCustomViewCell *cell = [_myTableView dequeueReusableCellWithidentifier:@"myCell"];

It has the right class, so you can modify it without an error, but it is not the cell that is visible in your UITableView . 它具有正确的类,因此您可以在没有错误的情况下对其进行修改,但它不是UITableView可见的单元格。

You should have some non-UI state that tells you what title to display, though: otherwise, scrolling a cell with the new title off the screen and back on will bring the old title back, which would be wrong. 你应该有一些非UI状态告诉你要显示什么标题,否则,在屏幕上滚动一个带有新标题的单元格并重新打开将带回旧标题,这是错误的。

To do what you are trying to achieve correctly, you need to implement the change in two places: first, the doSomething code should modify the model state responsible for changing the title, like this: 要做正确的尝试,您需要在两个地方实现更改:首先, doSomething代码应修改负责更改标题的模型状态,如下所示:

// This needs to be part of your model
NSMutableSet *rowsWithChangedTitle = ...

- (IBAction) doSomething:(id) sender { 

    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.myTableView]; 
    NSIndexPath *hitIndex = [self.myTableView indexPathForRowAtPoint:hitPoint];

    NSLog(@"%ld", (long)hitIndex.row); //This works and I can see the row which button placed
    [myModel.rowsWithChangedTitle addObject:@(hitIndex.row) ];
    [self.myTableView reloadData];
}

Then, you would need to change your tableView:cellForRowAtIndexPath: method like this: 然后,您需要更改tableView:cellForRowAtIndexPath:方法,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomViewCell *cell = [_myTableView dequeueReusableCellWithidentifier:@"myCell"];
    ...
    if ([myModel.rowsWithChangedTitle containsObject:@(indexPath.row)]) {
            cell.button.titleLabel.text = @"111";
            [cell.button setTitle:@"222" forState:UIControlStateNormal];
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM