Ios UITableViewCell重复行为与UIButton设置标题和单元格重用?
我的应用程序出现了一些非常不寻常的行为。我有一个基于服务器数据加载单元格的tableview,来自服务器的数据非常好,没有重复,应该是这样。然而,当我在表格中滚动时,我注意到我使用setTitle的UIButtons显示了以前滚动时显示的重复标题,就好像在调用setTitle的某些情况下,重复使用的单元格没有更新标题一样Ios UITableViewCell重复行为与UIButton设置标题和单元格重用?,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我的应用程序出现了一些非常不寻常的行为。我有一个基于服务器数据加载单元格的tableview,来自服务器的数据非常好,没有重复,应该是这样。然而,当我在表格中滚动时,我注意到我使用setTitle的UIButtons显示了以前滚动时显示的重复标题,就好像在调用setTitle的某些情况下,重复使用的单元格没有更新标题一样 UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:DETAILED_TAGS_FEED_CELL_
UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:DETAILED_TAGS_FEED_CELL_IDENTIFIER];
NSDictionary *tag = [self.tags objectAtIndex:indexPath.row];
ArgumentButton *nameButton = (ArgumentButton *)[cell viewWithTag:DETAILED_TAGS_FEED_CELL_TAG_NAME];
[nameButton addTarget:self action:@selector(tagSelected:) forControlEvents:UIControlEventTouchUpInside];
[nameButton setTitle:[NSString stringWithFormat:@"#%@", [tag objectForKey:@"tag"]] forState:UIControlStateNormal];
nameButton.argument = [tag objectForKey:@"tag"];
UILabel *totalFollowersLabel = (UILabel *)[cell viewWithTag:DETAILED_TAGS_FEED_CELL_TAG_TOTAL_FOLLOWERS];
totalFollowersLabel.text = [tag objectForKey:@"followers"];
UILabel *followersLabel = (UILabel *)[cell viewWithTag:DETAILED_TAGS_FEED_CELL_TAG_FOLLOWERS];
UIView *dividerView = (UIView *)[cell viewWithTag:DETAILED_TAGS_FEED_CELL_DIVIDER];
UILabel *totalProductsLabel = (UILabel *)[cell viewWithTag:DETAILED_TAGS_FEED_CELL_TAG_TOTAL_PRODUCTS];
totalProductsLabel.text = [tag objectForKey:@"products"];
UILabel *productsLabel = (UILabel *)[cell viewWithTag:DETAILED_TAGS_FEED_CELL_TAG_PRODUCTS];
if (self.allowFollow) {
totalFollowersLabel.frame = CGRectMake(30, totalFollowersLabel.frame.origin.y, totalFollowersLabel.frame.size.width, totalFollowersLabel.frame.size.height);
followersLabel.frame = CGRectMake(30, followersLabel.frame.origin.y, followersLabel.frame.size.width, followersLabel.frame.size.height);
dividerView.frame = CGRectMake(133, dividerView.frame.origin.y, dividerView.frame.size.width, dividerView.frame.size.height);
totalProductsLabel.frame = CGRectMake(157, totalProductsLabel.frame.origin.y, totalProductsLabel.frame.size.width, totalProductsLabel.frame.size.height);
productsLabel.frame = CGRectMake(157, productsLabel.frame.origin.y, productsLabel.frame.size.width, productsLabel.frame.size.height);
NSString *followButtonTitle = ([[tag objectForKey:@"followed"] isEqualToString:@"1"]) ? @"Unfollow" : @"Follow";
ArgumentButton *followButton = (ArgumentButton *)[cell viewWithTag:DETAILED_TAGS_FEED_CELL_TAG_FOLLOW];
followButton.argument = indexPath;
[followButton addTarget:self action:@selector(followSelected:) forControlEvents:UIControlEventTouchUpInside];
[followButton setTitle:followButtonTitle forState:UIControlStateNormal];
followButton.hidden = NO;
}
HorizontalScroll *scroll = (HorizontalScroll *)[cell viewWithTag:DETAILED_TAGS_FEED_CELL_TAG_SCROLL];
scroll.contentOffset = CGPointMake(-5, 0);
scroll.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
scroll.pagingEnabled = NO;
[scroll resetItems];
NSArray *items = [tag objectForKey:@"items"];
NSMutableArray *scrollItems = [[NSMutableArray alloc] init];
for (NSDictionary *item in items) {
NSArray *scrollItemXIB = [[NSBundle mainBundle] loadNibNamed:@"DetailedFeedScrollItemView" owner:self options:nil];
UIView *scrollItemView = [scrollItemXIB lastObject];
ArgumentButton *itemButton = (ArgumentButton *)[scrollItemView viewWithTag:DETAILED_FEED_SCROLL_ITEM_TAG_BUTTON];
[itemButton sd_setImageWithURL:[APIController resourceUrlForUserId:[item objectForKey:@"userId"] resourceName:[item objectForKey:@"thumbnail"]] forState:UIControlStateNormal];
[itemButton addTarget:self action:@selector(productSelected:) forControlEvents:UIControlEventTouchUpInside];
itemButton.argument = [item objectForKey:@"id"];
[scrollItems addObject:scrollItemView];
}
[scroll loadItems:scrollItems];
return cell;
注意:我在自己的XIB中使用UITableViewCells进行此操作,在viewDidLoad中设置如下
[self registerNib:[UINib nibWithNibName:@"DetailedTagsFeedTableViewCell" bundle:nil] forCellReuseIdentifier:DETAILED_TAGS_FEED_CELL_IDENTIFIER];
好像[nameButton setTitle:]在滚动时没有设置正确的标题?我尝试了一些调试,将followersLabel文本设置为与我们试图在nameButton上使用setTitle设置的值相同的值,并且在同一单元格中设置标签的.text属性时,该值是正确的。。非常混乱
参数按钮类没有什么特别之处。这只是一个简单的子类,它有一个公共属性,用于将自变量传递给目标/操作。很抱歉直截了当,但您的代码非常不可读,很难理解。我强烈建议现在是使用自定义单元格类的适当时机。看起来您使用的是自定义单元格类,那么为什么要对所有子视图使用标记而不是IBOutlets呢?如果这样做的话,您的代码会更短,更容易阅读。我还将取消整个if(self.allowFollow)子句,并将另一个单元格与那些已在xib文件中正确设置的子视图一起出列。^意识到这一点后,我才开始将所有内容转换为IBOutlets。谢谢你的建议