我的应用程序出现了一些非常不寻常的行为。我有一个tableview,它根据来自服务器的数据加载单元格。来自服务器的数据很棒,没有重复,应该如何。然而,当我滚动浏览我的表时,我注意到我使用setTitle的UIButton显示了之前在滚动时显示的重复标题,就好像在调用setTitle时某些情况下重用的单元格没有更新标题。
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属性时值为true。非常混乱。
Argument按钮类没什么特别的。只是一个简单的子类,它有一个公共属性,用于将参数与自身一起传递给目标/动作。