我正在尝试以编程方式添加一个UIStepper,HEre是我的代码: stepper不会出现。:
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语句只是用来定位单元格上的几个标签。
发布于 2011-12-02 12:06:05
[cell.contentView addSubview:stepper];编辑:在您的代码中,您拥有:
[stepper setFrame:CGRectMake(216, 91, 155, 25)];然而,你声称你的单元格有73px高,320px宽。
当前,您正在将步进器设置为155px宽,25px高,x原点为216,y原点为91。
步进器出现在y= 91的位置,在单元格的视图之外。
试试这个:
[stepper setFrame:CGRectMake(216, 22, 100, 30)];发布于 2011-12-02 15:32:21
使用initWithFrame:,这是以编程方式实例化作为UIView子类的类时指定的初始值设定项。
So:(调整标准44点高单元格的框架)
stepper = [[UIStepper alloc]initWithFrame:CGRectMake(206, 8, 94, 27)];我不知道为什么它还没有出现在你面前。为了测试代码,我创建了一个新的Xcode项目,类型为"Master/Detail“,目标是iPhone,然后我所做的唯一一件事就是像这样修改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;
}对于我来说,这段代码显示了默认的项目表,"Detail“行中有一个stepper。
发布于 2011-12-02 15:14:18
IOS5.0中引入了UIStepper控件,.you需要检查您是否在iOS5.0下使用它。
如需更多参考资料:
UIStepperControlExample
UIStepperControlAppleDocumentation
https://stackoverflow.com/questions/8351180
复制相似问题