我有一個UITableViewController,其中包含一個表,其中的單元格希望添加按鈕。這是一個自定義按鈕,它將充當複選框,具有正常狀態的圖像,以及用戶選擇時的圖像。不幸的是,每當我按下它時圖像都不會改變狀態,並且出於某種原因,它也不會調用目標方法。當在iOS應用程序中的UITableViewCell中按下時無法更改按鈕圖像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
TestObject *tObject = [myNSMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = tObject.testTitle;
testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(280, 57, 25, 25)];
[testButton setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateSelected];
[testButton setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
[testButton setUserInteractionEnabled:YES];
[testButton addTarget:self action:@selector(clickOnCheckButton:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:testButton];
return cell;
}
爲了記錄在案,我確實實現方法:
-(void)clickOnCheckButton:(id)sender {
NSLog(@"button has been pressed");
}
,並取得了一定的拼寫是否正確,這就是爲什麼我不明白爲什麼該方法不是原因當每個單元格中的按鈕被按下時調用。
我的表格輸出的另一個問題是,我第一個單元格里面沒有按鈕。然而,它下面的所有其他單元格都可以。
任何人都可以看到我做錯了什麼?
在此先感謝所有回覆的人。
你可能會想要研究的幾件事情:一個表格單元格對所有單元格內容的內容查看屬性,你可能想嘗試加入您的按鈕單元格contentView而不是單元格。你也可以用你的按鈕來替換單元的附件視圖。 –
感謝Andrew的建議。 accessoryView幫助。我現在能夠成功地將按鈕加載到單元並調用相應的方法。你可以發佈這個答案,以便我可以選擇它嗎? – syedfa
謝謝!我已經這樣做了。 –