我基本上和this question有同样的问题。但是你猜怎么了?这个问题没有任何好的答案。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if([indexPath row] == 0){
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
if( aCell == nil ) {
aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"];
aCell.textLabel.text = @"Colors";
aCell.selectionStyle = UITableViewCellSelectionStyleNone;
/*
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
aCell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
*/
UIStepper *stepperView = [[UIStepper alloc] initWithFrame:CGRectZero];
NSLog(@"The stepper is %@", stepperView);
aCell.accessoryView = stepperView;
[stepperView setMaximumValue:10];
[stepperView setMinimumValue:0];
[stepperView addTarget:self action:@selector(stepperChanged) forControlEvents:UIControlEventValueChanged];
NSLog(@"The stepper is %@", stepperView);
}
return aCell;
}
UITableViewCell *aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"];
NSString *s = @"foo";
s = [s stringByAppendingFormat:@"%d", [indexPath row]];
aCell.textLabel.text = s;
return aCell;
}因此,如果我将代码切换到注释掉的代码,就可以正常工作(显示UISwitch)。所以我想做的就是用UIStepper替换UISwitch,但它就是不显示。我讨厌objective-c。
哦,这两个打印行都显示值为(null)。在我创建它之后,它是空的吗?
编辑: iOS 5.0。我觉得答案可能与为什么会这样有关:
NSLog(@"%@", [[UISwitch alloc] init]);is not null while
NSLog(@"%@", [[UIStepper alloc] init]);为空。
发布于 2013-06-16 06:26:34
代码是在iOS 5.0中编译的,但模拟器是针对iPhone 4.3的。所以iPhone不知道UIStepper是什么,但知道UISwitch是什么。为什么它没有导致它崩溃(这可以为我节省很多时间)是一个谜。
发布于 2013-06-16 05:33:58
尝试通过以下方法创建UIStepper:
UIStepper* stepper = [[UIStepper alloc] init];
stepper.frame = CGRectMake(220, 10, 100, 10);
[cell.contentView addSubview: stepper];这将把步进器放在表格视图单元格的右侧。
我找到了这个答案in this related question。
https://stackoverflow.com/questions/17128048
复制相似问题