我在 Storyboard 的 View Controller 中设计了一个原型(prototype)单元格,但不幸的是单元格中的 UISlider
没有滑动。
注意:-此 Storyboard启用了自动布局。
设计:-
这是代码:- 在 UITableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if(indexPath.row!=self.arrSelectRecipientsImages.count-1 )
{
cell = [tableView dequeueReusableCellWithIdentifier:@"selectReciptenceTabCell" forIndexPath:indexPath];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:@"selectNearbyTabCell" forIndexPath:indexPath];
UISlider *sliderNearby = (UISlider *)[cell.contentView viewWithTag:199];
lblDistance = (UILabel *)[cell.contentView viewWithTag:190];
sliderNearby.userInteractionEnabled = YES;
[sliderNearby setThumbImage:[UIImage imageNamed:@"NearbyCircle"] forState:UIControlStateNormal];
[sliderNearby addTarget:self action:@selector(sliderNearbyAction:) forControlEvents:UIControlEventValueChanged];
}
return cell;
}
最佳答案
我认为代码运行良好。 也无需设置用户交互。
这是示例代码,您可以从中获得帮助。它与自动布局配合得很好。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
UILabel *lblTitle = (UILabel *)[cell viewWithTag:100];
lblTitle.text = @"Hi";
UISlider *slider = (UISlider *)[cell viewWithTag:101];
slider.value = 0.2;
slider.tag = indexPath.row;
[slider addTarget:self action:@selector(sliderNearbyAction:) forControlEvents:UIControlEventValueChanged];
return cell;
}
- (void)sliderNearbyAction:(UISlider *)sender{
NSLog(@"Slider %ld Value : %.2f",(long)sender.tag, sender.value);
}
带日志的输出:
自动布局截图:
关于ios - UISlider 不在原型(prototype)单元格中滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34488856/