2014-03-01 37 views
1

我正在創建帶有座位佈局圖的iOS應用程序。iOS座位圖佈局

試圖使用面向對象的方法,我爲TableLayoutObjects創建了一個類,因爲它們具有不同的屬性。

爲了將這些TableLayoutObjects放在屏幕上,我將它們表示爲UIButtons,它們是在我循環訪問TableLayoutObjects的數組時創建的。

- (void) loadTables 
{ 
    for (TableLayoutObjects *layoutObjs in arrTableLayoutObjects) 
    { 
    if ([layoutObjs.shape isEqualToString:@"r"]) { 
     // rectangle 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     ...... 

     if(layoutObjs.isInteractable) { 
      [button addTarget:self action:@selector(tableTouchedDown:) forControlEvents:UIControlEventTouchDown]; 
      [button addTarget:self action:@selector(tableTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside]; 
     } 
    } else { 
     // text only. use label 
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(layoutObjs.posX, layoutObjs.posY, layoutObjs.width, layoutObjs.height)]; 
     ...... 
    } 
    } 
} 

我的事件處理程序現在看起來像下面這樣。

// reverts back to original color and perform other instructions 
- (void) tableTouchedUpInside:(UIButton *) button 
{ 
    button.layer.backgroundColor = [UIColor colorWithWhite:0.2f alpha:.5f].CGColor; 
} 

我的問題是:我怎麼識別UIButtons他們TableLayoutObjects在更改按鈕顏色後的事件處理程序中,我還想獲取或設置所選TableLayoutObjects的某些屬性。我怎樣才能做到這一點?

回答

1

您可以將按鈕的tag設置爲相關項目的arrTableLayoutObjects數組中的索引。

或者,創建一個自定義類,該表將該表作爲參數,並且是該按鈕的目標。這個對象現在可以直接訪問按鈕和表項。

+0

意思我在'for循環裏面設置了'button.tag = ??'?如何在'for(... in ...)'循環中獲取當前索引?或者我應該將它更改爲'for(;;)'類型的循環? – chongzixin

+0

您可以更改循環,或使用計數器 – Wain

+0

謝謝〜我似乎更好地理解了以前的建議,並且想要嘗試一下。我發現'tag'被描述爲一個'你可以用來識別你的應用程序中的視圖對象的整數。'這會給與應用程序中的其他標記產生衝突嗎? – chongzixin

2

我認爲你的例子非常適合實施UICollectionView。帶按鈕的解決方案不那麼幹淨和更復雜。

+0

是的,這是我正在探索的一個選項。但後來因爲我的桌子沒有按照有序的方式佈置(例如不像教室或電影院),我在試圖將桌子擺放在正確的位置時遇到了問題。 – chongzixin

+0

你好@請和我一起做完你的座位佈局? –