iPhone/Objective-C-UITableViewCell中的UIButton重置问题
我在自定义的iPhone/Objective-C-UITableViewCell中的UIButton重置问题,iphone,uitableview,uibutton,Iphone,Uitableview,Uibutton,我在自定义的UITableViewCell中有一个ui按钮。我在界面生成器中添加了这个按钮 然后在cellForRowatineXpath中使用以下代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"GenericCellButton"; Ge
UITableViewCell
中有一个ui按钮。我在界面生成器中添加了这个按钮
然后在cellForRowatineXpath中使用以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"GenericCellButton";
GenericCellButton *cell = (GenericCellButton *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (GenericCellButton *)currentObject;
break;
}
}
}
[cell.inviteButton addTarget:self action:@selector(inviteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.inviteButton setTag:indexPath.row];
// This statement doesn't get called...?
if ([cell.inviteButton.titleLabel.text isEqualToString:@"Please Wait"]) {
[cell.inviteButton setEnabled:NO];
[cell.inviteButton setTitle:@"Please Wait" forState:UIControlStateDisabled];
}
...
这是以下方法:
方法:
- (void)followTapped:(id)sender {
UIButton *button = (UIButton *)sender;
[button setEnabled:NO];
[button setTitle:@"Please Wait" forState:UIControlStateDisabled];
// I then call a service method that then fires off a callback method that refreshes the `tableView`. This changes the button text to "Following".
}
就保存记录而言,一切都很顺利
问题是在启动回调方法之前;如果用户在刷新tableView
之前将可见单元格从视图中滚动出去,则会将按钮重置为“Follow”而不是“Please Wait”,这意味着如果用户再次点击“Follow”按钮,则会导致我的数据库出现问题
如果tableView
被滚动出视图,如何在重新加载之前阻止该特定单元格刷新?我尝试在单元格中添加一些逻辑来检查按钮文本是否为“请稍候”,但它似乎没有按预期工作。它仍在重置回“跟随”。UITableView类通常配置为循环使用单元格。如果您这样配置它,例如,通过遵循Apple提供的模板,那么使用该单元格来保存任何状态信息是行不通的。通常的方法是恢复cellforrowatinexpath:
方法中所需的任何状态,并将其应用于单元格。因此,点击下面的按钮:
方法以保存状态,然后在单元格中单击行索引路径:
应用[按钮设置启用:否]代码>和[按钮设置:@“请稍候”]代码>根据保存的状态。希望这会有所帮助(而且我没有错过问题的要点)。UITableView类通常配置为回收单元格。如果您这样配置它,例如,通过遵循Apple提供的模板,那么使用该单元格来保存任何状态信息是行不通的。通常的方法是恢复cellforrowatinexpath:
方法中所需的任何状态,并将其应用于单元格。因此,点击下面的按钮:
方法以保存状态,然后在单元格中单击行索引路径:
应用[按钮设置启用:否]代码>和[按钮设置:@“请稍候”]代码>根据保存的状态。希望这能有所帮助(而且我没有错过问题的重点。)
现在
现在
…我确实在cellforrowatinexpath:
中的按钮上设置了一个标记。我会设法拯救这个州,看看我会怎样做。谢谢您的帮助。请参阅我更新的问题代码,了解CellForRowatineXpath:
。我试图在单元格中检测“Please Wait”并将其设置为禁用,但它似乎不起作用。我确实在单元格中的按钮上设置了一个标记,用于rowatinexpath:
。我会设法拯救这个州,看看我会怎样做。谢谢您的帮助。请参阅我更新的问题代码,了解CellForRowatineXpath:
。我正在尝试检测单元格中的“请等待”并将其设置为禁用,但它似乎不起作用。谢谢,但我看不出如何selectedIndex=indexPath.row//在.h中创建此变量将在cellForRowAtIndexPath:
之外工作?您只需传递另一个参数,如:(void)followTapped:(id)sender and Index((NSIndexPath*)indexPath然后它就可以工作了…所以当你点击按钮时,你必须将当前的行索引传递给该方法,该方法进行了一些小的调整,但你把我引向了正确的方向。谢谢!在排序中,你只需要记住你以前选择的行。当表视图回收单元格时,然后告诉table view以另一种方式处理此单元格(您以前单击过按钮)。就是这样……谢谢,但我不知道当我们在cellforrowatinexpath:
之外时,selectedIndex=indexath.row;//在.h中创建此变量将如何工作:
?您只需要传递另一个参数,如:(void)followTapped:(id)发送方和索引((NSIndexPath*)indexPath然后它就可以工作了…所以当你点击按钮时,你必须将当前的行索引传递给该方法,该方法进行了一些小的调整,但你把我引向了正确的方向。谢谢!在排序中,你只需要记住你以前选择的行。当表视图回收单元格时,然后告诉table视图以另一种方式处理此单元格(您以前单击的按钮)。就是这样。。。
- (void)followTapped:(id)sender
{
UIButton *button = (UIButton *)sender;
[button setEnabled:NO];
[button setTitle:@"Please Wait" forState:UIControlStateDisabled];
selectedIndex = indexPath.row; //create this variable in .h file
// I then call a service method that then fires off a callback method that refreshes the `tableView`. This changes the button text to "Following".
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"GenericCellButton";
GenericCellButton *cell = (GenericCellButton *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (GenericCellButton *)currentObject;
break;
}
}
}
[cell.inviteButton addTarget:self action:@selector(inviteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.inviteButton setTag:indexPath.row];
// This statement doesn't get called...?
if (selectedIndex == indexPath.row)
{
[cell.inviteButton setEnabled:NO];
[cell.inviteButton setTitle:@"Please Wait" forState:UIControlStateDisabled];
}