2011-09-23 96 views
0

我正在製作一個iphone應用程序。在應用程序的一個視圖中,我實現了表視圖。在每個表格視圖單元格中,我放置了一個或兩個按鈕。現在我希望每當我點擊按鈕它給我的細胞的價值。如果有人對此有所瞭解,請告訴我,我該怎麼做?桌面單元格值

我已經實現爲顯示按鈕的代碼是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [rangetime objectAtIndex:indexPath.row]; 
    NSString *checkstatus = [finalstatusarray objectAtIndex:indexPath.row]; 
    if ([checkstatus isEqualToString:@"YES" ]) 
    { 
     UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton1.tag=1; 
     submitbutton1.frame = CGRectMake(200, 4, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"yes.png"]; 
     [submitbutton1 setImage:btnImage1 forState:UIControlStateNormal]; 
     cell.accessoryView = submitbutton1; 
     [submitbutton1 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    else if ([checkstatus isEqualToString:@"NO" ]) 
    { 
     UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton2.tag = 2; 
     submitbutton2.frame = CGRectMake(200, 4, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"no.png"]; 
     [submitbutton2 setImage:btnImage1 forState:UIControlStateNormal]; 
     [submitbutton2 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 
     cell.accessoryView = submitbutton2; 

    } 
    else if ([checkstatus isEqualToString:@"s" ])  
    { 
     UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton1.tag = 1; 
     submitbutton1.frame = CGRectMake(255, 5, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"no.png"]; 
     [submitbutton1 setImage:btnImage1 forState:UIControlStateNormal]; 
     [cell addSubview:submitbutton1];// = submitbutton1; 
     [submitbutton1 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 

     UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton2.tag = 2; 
     submitbutton2.frame = CGRectMake(285, 5, 28, 29); 
     UIImage * btnImage2 = [UIImage imageNamed:@"yes.png"]; 
     [submitbutton2 setImage:btnImage2 forState:UIControlStateNormal]; 
     [cell addSubview:submitbutton2]; 
     [submitbutton2 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    return cell; 
} 

enter image description here

Thanku非常多。

回答

1

你可以使用下面的代碼來完成。

我已經在UIButton的目標方法中編輯了你的答案,並添加了事件作爲參數,所以你可以得到該行的indexPath。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [rangetime objectAtIndex:indexPath.row]; 
    NSString *checkstatus = [finalstatusarray objectAtIndex:indexPath.row]; 
    if ([checkstatus isEqualToString:@"YES" ]) 
    { 
     UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton1.tag=1; 
     submitbutton1.frame = CGRectMake(200, 4, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"yes.png"]; 
     [submitbutton1 setImage:btnImage1 forState:UIControlStateNormal]; 
     cell.accessoryView = submitbutton1; 
     [submitbutton1 addTarget:self action:@selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    else if ([checkstatus isEqualToString:@"NO" ]) 
    { 
     UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton2.tag = 2; 
     submitbutton2.frame = CGRectMake(200, 4, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"no.png"]; 
     [submitbutton2 setImage:btnImage1 forState:UIControlStateNormal]; 
     [submitbutton2 addTarget:self action:@selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.accessoryView = submitbutton2; 

    } 
    else if ([checkstatus isEqualToString:@"s" ])  
    { 
     UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton1.tag = 1; 
     submitbutton1.frame = CGRectMake(255, 5, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"no.png"]; 
     [submitbutton1 setImage:btnImage1 forState:UIControlStateNormal]; 
     [cell addSubview:submitbutton1];// = submitbutton1; 
     [submitbutton1 addTarget:self action:@selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside]; 

     UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton2.tag = 2; 
     submitbutton2.frame = CGRectMake(285, 5, 28, 29); 
     UIImage * btnImage2 = [UIImage imageNamed:@"yes.png"]; 
     [submitbutton2 setImage:btnImage2 forState:UIControlStateNormal]; 
     [cell addSubview:submitbutton2]; 
     [submitbutton2 addTarget:self action:@selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    return cell; 
} 

- (void)updatestatus:(id)sender event:(UIEvent *)event 
{ 

    NSIndexPath *indexPath = [tblCityList indexPathForRowAtPoint:[[[event 
     touchesForView:sender] anyObject] locationInView:YourTableName]]; 

    **Now You can access the row value with the help of indexPath.row** 
} 
+0

非常感謝...它工作正常.. –

+0

它是我的榮幸... – SJS

0

首先,你必須修改下面的代碼...

[submitbutton1 addTarget:self action:@selector(updatestatus:) forControlEvents:UIControlEventTouchUpInside]; 

相反的,

[submitbutton1 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 

而且在updatestatus寫代碼如下:

- (IBAction) updatestatus:(id)sender 
    { 
     UIButton *btn = (UIButton*) sender; 
     UITableViewCell *cellSelected = (UITableViewCell*) [btn superview]; 
     NSIndexPath *idxPath = [tableView indexPathForCell:cell]; 
     // Your code... 
    } 
0

有以下代碼

-(void)showCellValue:(UIControl *)button{ 

UITableViewCell *cell = (UITableViewCell*)button.superview; 
NSIndexPath *indexPath =[aTableView indexPathForCell:cell]; 
NSLog(@"The cell value is %@",[tableData objectAtIndex:indexPath.row]); 

}

分配該方法showCellValue給按鈕的選擇器。 tableData是將數據加載到表的數組。並在你的情況下它的rangeTime數組 希望這會有所幫助。