我有TableView
和自定义类型的单元格。在原型cell
中,我有3个按钮。当我按下另一个按钮时,我希望能够更改按钮标题。我尝试这个:
- (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];
}
我做错了什么?
-(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;
}
您的代码不起作用的原因是此代码给了您一个完全无关的单元格:
MyCustomViewCell *cell = [_myTableView dequeueReusableCellWithidentifier:@"myCell"];
它有正确的类,所以您可以修改它而不会出错,但它不是UITableView
中可见的单元格。
你应该有一些非UI状态,告诉你要显示什么标题:否则,滚动一个带有新标题的单元格离开屏幕并重新打开会把旧标题带回来,这是错误的。
为了正确地实现您想要实现的目标,您需要在两个地方实现更改:首先,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];
}
然后,您需要像这样更改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];
}
...
}
添加操作处理程序,为单元格中的按钮提供触摸事件。像这样:
- (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
}
这里是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];
}
希望这能有所帮助。
替换行:
MyCustomViewCell *cell = [_myTableView dequeueReusableCellWithidentifier:@"myCell"];
带有:
MyCustomViewCell *cell = [_myTableView cellForRowAtIndexPath: hitIndex]
当您按下按钮时,标题会更改,但当您滚动且标题更改的行消失时,单元格会被回收,当您向后滚动时,单元格将从回收池中取出并设置为初始值。我的建议是创建一个数组,用于跟踪更改的行(标题),并且在cellForRowAtIndexPath:方法中,应该显示该数组中更改的标题。