2013-07-17 48 views
5

我有一個UITableViewCell(自定義單元格),其中我創建了一些按鈕和文本字段並將標籤分配給按鈕和文本字段。但是我點擊按鈕時無法獲得按鈕標題和文本框值。如何獲得單擊按鈕上的UITableviewcell(自定義單元格)值

cellForRowAtIndexPath

`[((CustomCell *) cell).btn setTag:rowTag]; 
[((CustomCell *) cell).textField2 setTag:rowTag+1];` 

-(IBAction)submitBtnAction:(UIControl *)sender 
{ 
    for (int i=0; i<[self->_dataArray count]; i++) 
    { 
     NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0]; 
     NSLog(@"myIP.row %d",myIP.row); 
     UITableViewCell *cell = [tblView1 cellForRowAtIndexPath:myIP]; 
     NSLog(@"tag %d",cell.tag); 
     UIButton *btn = (UIButton *)[cell.contentView viewWithTag:i]; 
     NSLog(@"btn text %@, tag %d",btn.titleLabel.text,btn.tag); 
     UITextField *tf = (UITextField *)[cell.contentView viewWithTag:i+1]; 
     NSLog(@"tf text %@, tag %d",tf.text,btn.tag); 

    } 
} 

我收到提示這樣

-[UITableViewCellContentView titleLabel]: unrecognized selector sent to instance 0x71844e0 
2013-07-17 13:48:29.998 Text[1271:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView titleLabel]: unrecognized selector sent to instance 0x71844e0' 

回答

11

我想你可以直接訪問你的單元格的btntextField2屬性,一旦你從cellForRowAtIndexPath:得到它。假設您正在創建並返回CustomCell的實例,只需將它轉換爲CustomCell而不是UITableviewCell即可。請參閱下面的修改代碼

-(IBAction)submitBtnAction:(UIControl *)sender 
{ 
     UIButton *button = (UIButton*)sender; 
     NSIndexPath *myIP = [NSIndexPath indexPathForRow:sender.tag inSection:0]; 
     //Type cast it to CustomCell 
     CustomCell *cell = (CustomCell*)[tblView1 cellForRowAtIndexPath:myIP]; 
     UIButton *btn = cell.btn; 
     NSLog(@"btn text %@, tag %d",btn.titleLabel.text,btn.tag); 

     UITextField *tf = cell.textField2; 
     NSLog(@"tf text %@, tag %d",tf.text,btn.tag); 
} 

希望幫助!

+0

感謝您的答覆......其工作很好 – user1831389

+0

@Amar ....可以使用此內容視圖的標籤獲取文本字段和按鈕的值.... [((CustomCell *)cell).contentView setTag:rowTag]; – user1831389

+0

@ user1831389我認爲在這種情況下,您應該使用'[cell viewWithTag:tag]'而不是'cell.contentView'來訪問button,textfield。 – Amar

0

通常,當您收到unrecognized selector錯誤,那是因爲你正在訪問已在內存中被替換的對象。在你的情況下,你可能正在訪問一個不可見的單元,所以contentview返回nil

當你做你的for循環,你似乎訪問所有細胞不可能不可見細胞。對我而言,您只能訪問可見單元格,因此無法識別的選擇器錯誤會導致您無法識別選擇器錯誤,因此無法訪問titleLabel

+0

謝謝回覆... – user1831389

0

爲標記值添加填充。否則,第一行標記爲0並且與內容視圖標記匹配,所有視圖標記默認爲0。因此,爲什麼你得到錯誤的觀點,當標籤等於0

#define PADDING 100 
[((CustomCell *) cell).btn setTag:PADDING + rowTag]; 
[((CustomCell *) cell).textField2 setTag:PADDING + rowTag+1]; 

不過,我會改變的解決方案根本就沒有使用增量標籤,但一個靜態標籤。您已經通過cellForRowAtIndexPath:獲得了特定單元格,您只需要該單元格的按鈕。

#define BUTTON_TAG 10 
#define TEXT_TAG 11 

cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
if (cell == nil) { 
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"] autorelease]; 
    [((CustomCell *) cell).btn setTag:BUTTON_TAG]; 
    [((CustomCell *) cell).textField2 setTag:TEXT_TAG]; 
} 

-(IBAction)submitBtnAction:(UIControl *)sender 
{ 
    for (int i=0; i<[self->_dataArray count]; i++) 
    { 
     NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0]; 
     NSLog(@"myIP.row %d",myIP.row); 
     UITableViewCell *cell = [tblView1 cellForRowAtIndexPath:myIP]; 
     UIButton *btn = (UIButton *)[cell.contentView viewWithTag:BUTTON_TAG]; 
     NSLog(@"btn text %@, tag %d",btn.titleLabel.text,btn.tag); 
     UITextField *tf = (UITextField *)[cell.contentView viewWithTag:TEXT_TAG]; 
     NSLog(@"tf text %@, tag %d",tf.text,btn.tag); 

    } 
} 
+0

感謝您的回覆.... – user1831389

2

簡單的方式來做到這一點:

-(IBAction)submitBtnAction:(UIControl *)sender 
{ 
    UIButton *senderButton = (UIButton *)sender; 

    NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0]; 

    CustomCell *cell = (CustomCell*)[tblView1 cellForRowAtIndexPath:myIP]; 

    NSLog(@"cell.textField -tag :%d",cell.textField2.tag); 

    NSLog(@"cell.btn -tag :%d",cell.btn.tag); 

} 
+0

感謝@ satheeshwaran..Its做工精細.... – user1831389

+0

任何時候@ user1831389。 – satheeshwaran

+0

@satheeshwaran ....有可能獲得文本字段,並使用標記該內容視圖按鈕的值.... [((CustomCell *)細胞).contentView setTag:rowTag]; – user1831389

0

問題是你設置你的看法與標籤0子視圖,而視圖的標籤屬性默認值是0,[someview viewWithTag:0]回報someview本身。

1

下面的代碼由「事件」來獲得indexPath PARAM可能會更好:

-(IBAction)submitBtnAction:(UIControl *)sender event:(id)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchPos = [touch locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPos]; 
    if(indexPath != nil) 
    { 
     //Todo: get model at indexPath, update cell or something other 
    } 
} 

submitBtn的目標選擇確認變更@selector(submitBtnAction:event:)

相關問題