2011-02-17 39 views

回答

3

是的。

如果你自己创建UIView,你可以做任何你想做的事情。

在你的情况下,它甚至不那么难。一些UILabels作为子视图和touchesDidSomething:withEvent:中的一些逻辑来确定触摸附近的标签。

还有一个委托方法,用于告知哪一部分被触摸。


我想我可能需要这样的东西,所以我决定尝试一下。

//myIndexSelectorView.m 

- (id)initWithFrame:(CGRect)frame andLabels:(NSArray *)l { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = [UIColor lightGrayColor]; 
     self.layer.cornerRadius = frame.size.width/2; 
     labels = [l retain]; 
     NSInteger count; 
     for (NSString *string in labels) { 
      CGFloat margin = 5.0f; 
      CGFloat yPosition = count*(frame.size.height/[labels count]); 
      UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(margin, yPosition, frame.size.width-2*margin, frame.size.height/[labels count])] autorelease]; 
      label.backgroundColor = [UIColor clearColor]; 
      label.textColor = [UIColor darkGrayColor]; 
      label.textAlignment = UITextAlignmentCenter; 
      label.text = string; 
      [self addSubview:label]; 
      count++; 
     } 
    } 
    return self; 
} 

- (void)touch:(UITouch *)touch { 
    CGPoint touchPosition = [touch locationInView:self]; 
    NSInteger index = touchPosition.y/(self.bounds.size.height/[labels count]); 
    NSLog(@"Touched: %@", [labels objectAtIndex:index]); 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    [self touch:touch]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    [self touch:touch]; 
} 

- (void)dealloc { 
    [labels release]; 
    [super dealloc]; 
} 

按预期工作,看起来类似的UITableView

enter image description here

像往常一样的指数选择,我没有检查的错误,这不应该是一个拷贝粘贴&解决方案。

0

如果你正在谈论键盘,那么它不能被定位为默认位置,原因是苹果不允许改变它的位置。希望能帮助你。谢谢。

+0

我认为他意味着你可以在图像中看到的字母栏在这里:http://developer.apple.com/library/ios/DOCUMENTATION/UserExperience /Conceptual/TableView_iPhone/Art/types_of_table_views.jpg而不是键盘。 – 2011-02-17 07:54:46

+0

不,我不是在谈论小键盘,我想在UIView右侧的垂直字母表列表中使用UITableview – Aqueel 2011-02-17 07:58:36

1

您可以使用不同的UILabels,并为它们设置标签。现在检测适当的标签上的触摸,你可以使用UITapGestureRecognizer类