我遇到这样的问题:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
tvcCategory *cell = (tvcCategory*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"- %i ", indexPath.item);
if (selectedIndex.item != indexPath.item) {
[btnEdit removeFromSuperview];
[btnTransact removeFromSuperview];
objSession.catId = cell.lblId.text;
btnEdit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnEdit.frame = CGRectMake(91, 72, 73, 35);
[btnEdit setTitle:@"Edit" forState:UIControlStateNormal];
[btnEdit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnEdit setBackgroundImage:buttonImage forState:UIControlStateNormal];
[btnEdit setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[btnEdit addTarget:self action:@selector(moveToEditingScreen) forControlEvents:UIControlEventTouchUpInside];
btnTransact = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTransact.frame = CGRectMake(176, 72, 95, 35);
[btnTransact setTitle:@"Transact" forState:UIControlStateNormal];
[btnTransact setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnTransact setBackgroundImage:buttonImage forState:UIControlStateNormal];
[btnTransact setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[btnTransact addTarget:self action:@selector(moveToTransactionScreen) forControlEvents:UIControlEventTouchUpInside];
[tableView reloadData];
[cell.contentView addSubview:btnEdit];
[cell.contentView addSubview:btnTransact];
lastClickedCell = cell;
self.selectedIndex = indexPath;
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:-1 inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationFade];
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
[tableView reloadData];
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
}
我真的很感激
答案 0 :(得分:0)
你所面临的问题,就像那些人在评论中所揭示的那样,在表格视图方法中你需要重复使用单元格来优化性能,所以你需要使用已经使用过的单元格并调整它到你期望的细胞状态。
可能在你的cellForRowAtIndexPath
中,你只需要分配它并在你没有单元格时进行调整,你需要调整它,你有一些选择,比如总是把它调整到初始状态或者您可以检查按钮是否显示,并且仅在这种情况下重新设计您的单元格。
所以在你的课堂上,你可以使用这种方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
} else {
if([selectedIndex isEqual:indexPath]){
//Add your buttons and adjust the cell to show the extra buttons
} else {
if(your selected index isn't in the screen anymore){
[btnEdit removeFromSuperview];
[btnTransact removeFromSuperview];
}
//your others adjusts
}
}
}
修改强>
[[yourTable indexPathsForVisibleRows] containsObject:selectedIndexPath];
此方法返回一个数组,您需要检查所选的索引是否在其中。 有关UITableView的更多信息,请访问documentation。