我正在開發具有每個表格視圖單元格中動態數量按鈕功能的應用程序。按鈕數量因行而異。如何獲取表格視圖單元格上的按鈕動態按鈕的按鈕事件
我想給圖像一個按鈕來點擊。但我無法得到它,所以任何人都可以幫助我改變所選按鈕的圖像。
我正在開發具有每個表格視圖單元格中動態數量按鈕功能的應用程序。按鈕數量因行而異。如何獲取表格視圖單元格上的按鈕動態按鈕的按鈕事件
我想給圖像一個按鈕來點擊。但我無法得到它,所以任何人都可以幫助我改變所選按鈕的圖像。
您應該爲您的按鈕添加標籤,並在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法中爲其分配indexPath.row
。
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setTitle:@"Button" forState:UIControlStateNormal];
[yourButton addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
yourButton.tag=indexPath.row;
yourButton.frame = CGRectMake(5, 5, 100, 30);
[cell addSubview:yourButton];
之後,定義buttonSelected:
方法。
-(void)buttonSelected:(id)sender{
UIButton *button = (UIButton *)sender;
NSLog(@"buttonSelectd: %d",button.tag);
//implement your code what you want.
}
我認爲這會對您有所幫助。
感謝prasad爲你的答案我知道它工作正常,但我的問題是每個單元格上的按鈕數是不同的手段(單元格1有2個按鈕,單元格2 6按鈕)按鈕的數量不固定,所以我有麻煩,這樣做這裏是我的示例代碼添加按鈕單元格 – user1579258 2012-08-17 05:01:08
for(int i = 0; i <[Array count]; i ++) { optionButton = [[UIButton alloc] init]; optionButton = [UIButton buttonWithType:UIButtonTypeCustom]; optionButton.frame = CGRectMake(5,80,22,18); [optionButton addTarget:self action:@selector(buttonPresssed :) forControlEvents:UIControlEventTouchUpInside]; optionButton.tag = i; [cell addSubview:optionButton]; } – user1579258 2012-08-17 05:07:58
按鈕的數量取決於陣列併發,所以我無法追蹤哪個按鈕被點擊,還有一件事我必須一起收集選擇按鈕值,所以請你可以告訴我如何做到這一點。預先感謝 – user1579258 2012-08-17 05:09:55
請顯示相關的代碼,你已經嘗試過:-) – 2012-08-11 05:17:54