UISlider 损坏

Posted

技术标签:

【中文标题】UISlider 损坏【英文标题】:UISlider corrupted 【发布时间】:2012-09-30 11:33:51 【问题描述】:

我以编程方式在每个 UITableViewCell 中嵌入了一个 UISlider,但我注意到这些 UISlider 中的前 2 个总是损坏,如下图所示。

当我第一次滑动 UISlider 按钮时,它的一部分会落在后面。有没有其他人遇到过同样的问题?如果您需要查看我的代码,请告诉我。

除了这个问题之外,连接到它旁边的 UISlider 的 UIlabel 效果很好。

更多详情: 我的 cellForRowAtIndexPath 函数如下。请查找我添加 UISlider 的部分的最后第二段。我还怀疑我可能会在单元格中添加超过 1 个滑块。我通过检查该行的 NSLog 输出来检查每个滑块是否只添加一次:

NSLog(@"initiating slider at section %d row %d", indexPath.section, indexPath.row);

每个部分和行的组合都是独一无二的!

我还觉得很奇怪,只有前 2 行中的 UISlider 会受到此影响,而不管我的表中有多少 tableViewCell-rows。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

//creating a basic valid cell object
static NSString *cellIdentifier = @"ActivitiesTVCcell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];


if(indexPath.section == 0)
    NSArray *rowNames = [self.phpReply_ActivitiesTVC componentsSeparatedByString:@"\n"];
    cell.textLabel.text = [rowNames objectAtIndex:indexPath.row+2];

else if (indexPath.section == 1)
    if (indexPath.row == 0) 
        cell.textLabel.text = @"Seated";
    
    else if (indexPath.row == 1)
        cell.textLabel.text = @"Standing";
    


cell.selectionStyle = UITableViewCellSelectionStyleNone;



//start code addition of graph button stuff
UIButton *graphButton = [UIButton buttonWithType:UIButtonTypeCustom];
[graphButton setFrame:CGRectMake(224, 0, 43, 37)];
[graphButton setBackgroundImage:[[UIImage imageNamed:@"graph icon.png"] stretchableImageWithLeftCapWidth:1.0 topCapHeight:1.0] forState:UIControlStateNormal];
[graphButton setBackgroundImage:[[UIImage imageNamed:@"graph icon.png"] stretchableImageWithLeftCapWidth:1.0 topCapHeight:1.0] forState:UIControlStateSelected];

[graphButton addTarget:self
                action:@selector(graphButtonPressed:)
      forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:graphButton];
//end code addition of graph button stuff


//start code addition of video button stuff
UIButton *videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[videoButton setFrame:CGRectMake(300, 0, 43, 37)];
[videoButton setBackgroundImage:[[UIImage imageNamed:@"videoicon.png"] stretchableImageWithLeftCapWidth:1.0 topCapHeight:1.0] forState:UIControlStateNormal];
[videoButton setBackgroundImage:[[UIImage imageNamed:@"videoicon.png"] stretchableImageWithLeftCapWidth:1.0 topCapHeight:1.0] forState:UIControlStateSelected];

[videoButton addTarget:self action:@selector(videoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:videoButton];
//end code addition of video button stuff


//start code for addition of enable switch
UISwitch *activityEnableSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(376, 7, 0, 0)]; //size components of CGRect are ignored
[activityEnableSwitch setOn:NO animated:YES];
[activityEnableSwitch addTarget:self
                         action:@selector(enableSwitchStateChanged:)
               forControlEvents:UIControlEventValueChanged];
[cell addSubview:activityEnableSwitch];
//end code for addition of enable switch

//start code for addition of target angle text field
UITextField *targetAngleTextField = [[UITextField alloc] initWithFrame:CGRectMake(476, 7, 50, 25)];
targetAngleTextField.borderStyle = UITextBorderStyleRoundedRect;
targetAngleTextField.font = [UIFont systemFontOfSize:15];
targetAngleTextField.textColor = [UIColor blackColor];
targetAngleTextField.placeholder = @"30";
targetAngleTextField.autocorrectionType = UITextAutocorrectionTypeNo;
targetAngleTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
targetAngleTextField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
targetAngleTextField.keyboardType = UIKeyboardTypeNumberPad; //no num pad on ipad :(
//[targetAngleTextField becomeFirstResponder];
targetAngleTextField.delegate = self;

[cell addSubview:targetAngleTextField];
//end code for addition of target angle text field

//start code for addition of degree text label
UILabel *degreeLabel = [[UILabel alloc] initWithFrame:CGRectMake(532, 6, 10, 10)];
degreeLabel.text = @"o";
degreeLabel.font = [UIFont systemFontOfSize:10];
[cell addSubview:degreeLabel];
//end code for addition of degree text label

//start code for addition of progression level UISlider
NSLog(@"initiating slider at section %d row %d", indexPath.section, indexPath.row);
UISlider *progressionLevelSlider = [[UISlider alloc] initWithFrame:CGRectMake(576, 7, 150, 25)];
progressionLevelSlider.minimumValue = 0;
progressionLevelSlider.maximumValue = 9;
progressionLevelSlider.continuous = YES;
progressionLevelSlider.value = 3;

[progressionLevelSlider addTarget:self
           action:@selector(progressionSliderValueChanged:)
 forControlEvents:UIControlEventValueChanged];

[cell addSubview:progressionLevelSlider];
//end code for addition of progression level UISlider

//start code for addition of level ? text label
UILabel *progressionLevelLabel = [[UILabel alloc] initWithFrame:CGRectMake(782, 13, 10, 20)];
progressionLevelLabel.text = [NSString stringWithFormat:@"%d", 2];
progressionLevelLabel.font = [UIFont systemFontOfSize:17];
progressionLevelLabel.tag = 121; //tag represents this particular UIlabel, so that we can set it from outside this function.
[cell addSubview:progressionLevelLabel];
//end code for addition of level ? text label


[cell setTag:(indexPath.section)*100+indexPath.row];

return cell;

这是我的 sliderValueChanged 函数。我将这段代码放在这里,因为只有在拖动滑块(以更改其值)后才能看到“损坏”。

-(void)progressionSliderValueChanged: (UISlider *)sender 
int section = ([sender superview].tag)/100;
int row = [sender superview].tag%100;

int value = sender.value;

NSLog(@"slider value at section %d row %d was slided to %d",section, row, value);

UITableViewCell *superViewOfSlider = (UITableViewCell *)[sender superview];
UILabel *progressionLevelLabel = (UILabel *)[superViewOfSlider viewWithTag:121];
progressionLevelLabel.text = [NSString stringWithFormat:@"%d", value];
[superViewOfSlider addSubview:progressionLevelLabel];

【问题讨论】:

您几乎可以肯定会在 cellForRowAtIndexPath 中的单元格中添加多个滑块:请编辑您的问题以包含添加滑块的代码。 【参考方案1】:

每次重复使用一个单元格时,您都会向该单元格添加一组全新的子视图。讲述创建这些子视图的所有代码并将其移动到您创建单元格的块中。 那个块之后,你只需要找到你需要的子视图(例如使用标签)并设置这些子视图的值。

【讨论】:

以上是关于UISlider 损坏的主要内容,如果未能解决你的问题,请参考以下文章

UISlider thumbImage 无法退出 uislider 框架

UISlider的基本使用方法

UISlider 带标签怎么调用?

UISlider

设置 UISlider

UISlider 设置值