问题
I have designed a prototype cell in a Storyboard's view controller, but unfortunately the UISlider
in the cell is not sliding.
Note:- Autolayout is enabled for this storyboard.
Design:-
Here is code:- to render the cell in 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;
}
回答1:
The code is working fine I think. Also no need to set user interaction.
Here is the example code may you get help form it. It is working fine with autolayout.
- (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);
}
OUTPUT with Log :
Autolayout Screenshot :
回答2:
Well, the problem was very silly, I have not provided height for cell in different index-path.That's why the last cell i.e the slider cell was showing but it's height was 30, so any tap or gesture outside that region was not responding.
来源:https://stackoverflow.com/questions/34488856/uislider-not-sliding-in-prototype-cell