我有一個動詞共軛應用程序,它在表格的第一個單元格中顯示動詞翻譯。目前翻譯列表只是一個字符串(以逗號分隔的列表),但我想將其更改爲具有可點擊的按鈕。我有一個圍繞向單元視圖添加按鈕的玩法,但沒有太多的成功,但是我對定製單元格的唯一體驗是使用了特定的定位,所以我不確定如何在單元格內實現動態按鈕列表(寬度變化)。將按鈕動態添加到可用視圖單元 - iPhone應用程序
任何幫助非常感謝。
乾杯。
我有一個動詞共軛應用程序,它在表格的第一個單元格中顯示動詞翻譯。目前翻譯列表只是一個字符串(以逗號分隔的列表),但我想將其更改爲具有可點擊的按鈕。我有一個圍繞向單元視圖添加按鈕的玩法,但沒有太多的成功,但是我對定製單元格的唯一體驗是使用了特定的定位,所以我不確定如何在單元格內實現動態按鈕列表(寬度變化)。將按鈕動態添加到可用視圖單元 - iPhone應用程序
任何幫助非常感謝。
乾杯。
製作一個UIView
對象。將您的UIButton
對象設置爲此父視圖的子視圖。
一旦你有了,你可以將這個父視圖添加到單元的contentView
屬性。
在-tableView:cellForRowAtIndexPath:
委託方法中,您可以在將其添加到單元的contentView
之前,根據某些條件狀態生成父視圖(以及內部按鈕的數量和類型)。
至於這樣做以編程方式,只要一些條件改變,運行[tableView reloadData]
或類似的方法來刷新表視圖及其單元格。
完成下列步驟
-(CGRect)placeAButton:(NSString*)textFromField withCell:(UITableViewCell*)cell
{
CGSize theSize;
CGSize constraintSize;
// Create a button and add as subview to cell
// Get coordinates to place the button
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
button.titleLabel.font = [UIFont systemFontOfSize:MEDIUM_FONT_SIZE];;
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:textFromField forState:UIControlStateNormal];
UIImage *buttonBkground;
buttonBkground = [UIImage imageNamed:@"blueButton.png"];
UIImage *newImage = [buttonBkground stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
[self.tableView reloadData]
}
你必須同時動態創建按鈕照顧細胞的寬度,高度等。
我剛剛爲我的新iPad應用做了這個,所以我想我會粘貼代碼。
// NOTE - This is within a loop
// Add the translation as a button
UIButton *translationButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
translationButton.backgroundColor = [UIColor clearColor];
translationButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
[translationButton setTitle:translation forState:UIControlStateNormal];
[translationButton addTarget:self action:@selector(translationSearch:) forControlEvents:UIControlEventTouchUpInside];
// Work out required size
fontSize = [translationButton.titleLabel.text sizeWithFont:translationButton.titleLabel.font];
CGRect buttonFrame = CGRectMake(xOffset, 15, fontSize.width + 20.0, 24);
[translationButton setFrame:buttonFrame];
// Now set the new x Offset
xOffset = buttonFrame.origin.x + buttonFrame.size.width + 6.0;
[verbView addSubview:translationButton];
乾杯。
如果我們沒有按鈕標題來區分按鈕並且它們沒有任何文本,可以做些什麼。在我的情況下,我只是使用圖像,而不是按鈕標題。在這種情況下可以做些什麼? – 2011-01-18 13:35:15
如果你正在使用圖像,那麼我假設你知道它們的大小。在這種情況下,您可以簡單地定期放置它們。上面的代碼使用fontSize.width來計算偏移量。你應該簡單地使用圖像的寬度。 – alan 2011-01-21 02:02:10
你可以做到這一點的視圖 - 控制的cellForRowAtIndexPath函數內部:
var btn = UIButton(frame: CGRectMake(100, 100, 100, 20));
btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
btn.setTitle("My Button", forState:UIControlState.Normal);
btn.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside);
//add the buttons to the cell
cell.contentView.addSubview(btn)
添加其他功能相同的類將接收按鈕竊聽事件
func buttonTapped(sender: UIButton!)
{
println("Button Tapped")
}
當你向上或向下移動你的單元格時,上面的按鈕再次被重新創建。 – 2017-11-29 06:36:39
在檢查按鈕是否存在後,在函數外部創建按鈕並添加到單元格中。如果有很多單元格保存按鈕的地圖或索引。 – srinivas 2017-11-30 09:43:37
您好,感謝您的答覆。我有一個裂縫,但沒有得到任何東西。當我有機會的時候,我會再去一次,對不起,我無法及時回覆(或者花費足夠的時間解決我仍然遇到的問題)。我會盡力做到這一點。 – alan 2009-10-17 10:23:12