2013-01-15 59 views
0

我們的設計人員提出了一個很好的設計,其中UITableView右側的索引要比UITableView本身高。它還有一些用於搜索的自定義圖像以及比默認實現更好的字體/顏色。但是,由於UITableView不支持自定義其索引(我知道),我一直試圖讓這個手動進行。該UI看起來是這樣的:創建自定義UITableView索引iOS

enter image description here

這應該是一個簡單的任務,但我有一個時間地獄使其工作。我用UIButton從上到下排列了一個UIView來表示我的索引。作爲用戶拖入/拖出UIButtons的目標,我會將UITableView跳到正確的部分。

我已經搜索過,看起來這樣做的事情是聽UIControlEventTouchDragOutside和UIControlEventTouchDragEnter事件來知道什麼時候我進/出UIButtons之一。

爲此,我已經設置了按鈕的整個列表,這是我在viewDidLoad中初始化像這樣的IBOutletCollection:

@property (retain, nonatomic) IBOutletCollection(UIButton) NSArray* indexButtons; 


@implementation ViewBeerListViewController 
... 
@synthesize indexButtons = _indexButtons; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    ... 

    // Go through the index buttons and set the change function 
    for (int i = 0; i < [self.indexButtons count]; ++i) 
    { 
     UIButton* button = [self.indexButtons objectAtIndex:i]; 
     [button addTarget:self action:@selector(touchDragOutside:) forControlEvents:UIControlEventTouchDragOutside]; 
     [button addTarget:self action:@selector(touchDragEnter:) forControlEvents:UIControlEventTouchDragEnter]; 
    } 
} 

The functions touchDragOutside and touchDragEnter look like this: 

- (IBAction)touchDragOutside:(UIButton*)sender 
{ 
    NSLog(@"Button %@ touch dragged outside", [sender titleLabel].text); 
} 

- (IBAction)touchDragEnter:(UIButton*)sender 
{ 
    NSLog(@"Button %@ touch dragged enter", [sender titleLabel].text); 
} 

這一切都建立並運行。不過,我似乎只能得到我開始觸摸的按鈕的事件。例如。如果我觸摸字母「G」,並開始上下拖動,則只會看到觸摸日誌拖出「G」並拖入「G」。當我瀏覽它們時,我沒有收到任何其他UIButton的事件。

任何幫助解決這個問題將非常感激。我一直被困在很長時間以來似乎是非常微不足道的問題上。

謝謝!

回答

1

而不是使用UIButtons製作它們,請嘗試創建包含每個字母的UILabels的自定義UIView。然後,在自定義UIView類,覆蓋以下內容:

– touchesBegan:withEvent: 
– touchesMoved:withEvent: 
– touchesEnded:withEvent: 

,並利用這些來確定哪些被觸摸的標籤。在touchesMoved,例如,你可以有:

UITouch * touch = [touches anyObject]; 
CGPoint point = [touch locationInView:self]; 
for(UIView * indexView in self.indexViews) { 
    if(CGRectContainsPoint(indexView.frame,point)) { 
     //indexView was touched. do something here 
     break; 
    } 
} 

注意,你也可以使用一個UIImageView的頂部而不是一個UILabels,這仍然可以工作。

+0

有趣。今晚我會試試這個。感謝您的建議! – Jason

+0

酷,這將工作得很好:)謝謝你好先生! – Jason

+0

沒問題。祝你好運。 –