我有tableView,在其中一行中我有一个水平scrollView和一些图像(图像在该行的不同视图中),在水平scrollView中我添加了几个按钮(10个按钮)。当用户单击scrollView中的任何按钮时,我都会调用reloadData。因为我调用了reloadData,所以水平scrollView会重新加载新的图像数据。现在我想如果用户滚动到末尾并单击scrollView中的最后一个按钮,用户应该能够看到选定的按钮,而不是第一个按钮。我不知道如何使用scrollRectToVisible:animated来实现这一点。
下面是我在scrollView上添加按钮的代码。
-(void)addColorFiltersOnView:(UIView *)colorView{
if (_colorFilterView) {
[_colorFilterView removeFromSuperview];
}
self.colorFilterView = [[UIScrollView alloc] initWithFrame:colorView.bounds];
[_colorFilterView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[_colorFilterView setBackgroundColor:samergb(238)];
[_colorFilterView setShowsHorizontalScrollIndicator:NO];
int addedCount = 0;
int i=0;
CGFloat x = 10, y = 5;
if ([_colorFilters count] < 1) {
return;
}
for (i=0; i<[_colorFilters count] - 1; i++) {
id item = [_colorFilters objectAtIndex:i];
if ([item isKindOfClass:[NSString class]]){
if ([self.prodVIPDictionary.PDcolor isEqualToString:(NSString*)item] && i != 0) {
continue;
}
NSInteger itemsCount = [[_colorFilters objectAtIndex:i+1] integerValue];
if (itemsCount <= 0) {
break;
}
TTTAttributedLabel *itemLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
[itemLabel setNumberOfLines:1];
[itemLabel setTextAlignment:NSTextAlignmentCenter];
[itemLabel setFont:[UIFont systemFontOfSize:14]];
if ([_selectedColor isEqualToString:(NSString*)item])
[itemLabel setTextColor:[UIColor whiteColor]];
[itemLabel setText:[(NSString*)item capitalizedString]];
[itemLabel setBackgroundColor:samergb(255)];
itemLabel.layer.borderWidth = 1;
itemLabel.layer.masksToBounds = YES;
itemLabel.layer.cornerRadius = 10;
itemLabel.layer.borderColor = samergb(210).CGColor;
[itemLabel sizeToFit];
[_colorFilterView addSubview:itemLabel];
CGRect itemFrame = itemLabel.frame;
itemFrame.size.width += 20;
[itemLabel setFrame:itemFrame];
UIButton *optionBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, itemLabel.frame.size.width + 6, 26)];
[optionBtn setTag:[_colorFilters indexOfObject:item]];
[optionBtn addTarget:self action:@selector(optionColorClicked:) forControlEvents:UIControlEventTouchUpInside];
[_colorFilterView addSubview:optionBtn];
[itemLabel setFrame:optionBtn.frame];
if ([_selectedColor isEqualToString:(NSString*)item]) {
[optionBtn setEnabled:NO];
[itemLabel setBackgroundColor:[LRUtils lrGreenColor]];
}
addedCount ++;
x += optionBtn.frame.size.width+5;
if (addedCount >= 20) {
break;
}
}else{
continue;
}
}
[_colorFilterView setContentSize:CGSizeMake((x + 10),colorView.frame.size.height)];
[colorView addSubview:_colorFilterView];
}发布于 2016-01-08 07:55:25
您可以在表视图上调用此方法,而不是调用reloadData:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
您需要创建一个包含表视图中除滚动视图和按钮所在行之外的所有IndexPaths的NSArray。这样,滚动视图就不会被重置。
https://stackoverflow.com/questions/34662301
复制相似问题