【问题标题】:UIStepper not showing upUIStepper 没有出现
【发布时间】:2011-12-02 02:45:49
【问题描述】:

我试图以编程方式添加 UIStepper,这是我的代码:步进器不会显示。 :

     stepper =  [[UIStepper alloc]init];
     [stepper setFrame:CGRectMake(216, 91, 155, 25)];
     [stepper setMinimumValue:0];
     [cell addSubview:stepper];

谢谢!

CELLFORROWATINDEXPATH:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
     {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

         UIStepper * myStepper =  [[UIStepper alloc]initWithFrame:CGRectMake(206,20,94,27)];
         [myStepper setMinimumValue:0];
         [cell addSubview:myStepper];

         cellLabel = [[UILabel alloc]init];
         [cellLabel setFrame:CGRectMake(65, 22, 27, 27)];
         [cellLabel setText:@"1"];
         [cell.contentView addSubview:cellLabel];
    } 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];    
   // cell.textLabel.text = [cells objectAtIndex:indexPath.row];
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle5.png"]]) {
        [cellLabel setFrame:CGRectMake (175, 22, 30, 30)];
    }
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle4.png"]]) {
        [cellLabel setFrame:CGRectMake (150, 22, 30, 30)];
    }
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle3.png"]]) {
        [cellLabel setFrame:CGRectMake (120, 22, 30, 30)];
    }
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle2.png"]]) {
        [cellLabel setFrame:CGRectMake (90, 22, 30, 30)];
    }
return cell;
}

return cell; 正上方的 if 语句只是为了在单元格上放置一些标签。

【问题讨论】:

  • 告诉我们更多关于细胞的信息。

标签: iphone ios cocoa-touch uitableview


【解决方案1】:
[cell.contentView addSubview:stepper];

编辑: 在您的代码中,您有:

 [stepper setFrame:CGRectMake(216, 91, 155, 25)];

但您声称您的单元格高 73 像素,宽 320 像素。

目前,您将步进器设置为宽 155 像素,高 25 像素,x 原点为 216,y 原点为 91。

步进器,出现在 91 的 y 处,位于单元格视图之外。

试试这个:

[stepper setFrame:CGRectMake(216, 22, 100, 30)];

【讨论】:

  • 不起作用 :( 它与我的 setFrame:CGRect 有什么关系吗?谢谢
  • 你的单元格大小是多少?该框架是否适合单元格的边界?
  • @ThomasW 高度是 73px 高和 320px 宽!!
  • @iProRage,请看看我的编辑。您在单元格外显示步进器...
  • @DanielAmitay 好的,我试过了,但是它仍然没有显示出来与cellForRow 方法完全相同的代码,它没有出现在这个应用程序中。我在我的问题中添加了我所有的cellForRow。请你看看它是否有什么问题?谢谢帮忙!!
【解决方案2】:

使用initWithFrame:,这是在以编程方式实例化作为UIView 子类的类时指定的初始化器。

所以:(调整标准 44 点高单元格的框架)

stepper =  [[UIStepper alloc]initWithFrame:CGRectMake(206, 8, 94, 27)];

我不知道为什么它仍然没有出现在你面前。为了测试代码,我创建了一个针对 iPhone 的“Master/Detail”类型的新 Xcode 项目,然后我唯一做的就是像这样更改 tableView:cellForRowAtIndexPath: 方法。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(206, 8, 94, 27)];
        [stepper setMinimumValue:0];
        [cell addSubview:stepper];
    }
    // Configure the cell.
    cell.textLabel.text = NSLocalizedString(@"Detail", @"Detail");
    return cell;
}

对我来说,这段代码显示了默认的项目表,并且“详细信息”行中有一个步进器。

【讨论】:

  • 感谢您的回复,但是我添加了这一行并删除了我的stepper = [[UIStepper alloc]init]; 声明。它仍然拒绝出现!你知道这是为什么吗?谢谢
  • 对不起,它不是 44px 高,我很笨,在界面生成器中,我将高度设置为 73,而不是 43
  • 哈哈该死的人! :/我将代码固定为您上面所说的方法,并固定了坐标。它仍然不会显示!
  • 把这个放在代码NSLog(@"The cell is:%@",cell);之前
  • 好的,我知道了,控制台说:The cell is:<UITableViewCell: 0x6947cf0; frame = (0 0; 320 44); layer = <CALayer: 0x6947dd0>>
【解决方案3】:

ios 5.0 引入了UIStepper 控件,你需要检查你在ios 5.0 下没有使用它。

更多参考:-

UIStepperControlExample

UIStepperControlAppleDocumentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2015-01-19
    • 2012-09-21
    • 2021-02-15
    • 2014-05-31
    相关资源
    最近更新 更多