How to change UIButton title which placed in prototype cell?
I have TableView with custom type of cells. In prototype cell I have 3 buttons. 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;
}
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
.
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.
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:
// 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:
- (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];
}
...
}
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:
- (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.
链接地址: http://www.djcxy.com/p/60504.html上一篇: 滚动查看显示在多个表格单元格中