2013-01-23 34 views
0

可能重複:
How to know the UITableview row number我如何理解哪個tableViewCell包含點擊的按鈕?

我有一個的tableView並在此的tableView一個tableViewCell。我爲tableViewCell定義了另一個名爲CustomCell的類,以便我編寫必要的自定義設置並創建一個按鈕(在此單元格上)。當單擊tableViewCell上的按鈕時,我想知道哪個tableViewCell包含該按鈕,以便我可以僅對該單元格進行必要的更改(其中包含單擊的按鈕)

如何理解哪個tableViewCell包含被點擊的按鈕?

+0

把標籤放到每個按鈕上怎麼樣? –

回答

0

一種策略是分配與一個標籤行號到被按下的按鈕:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexpath { 
    YourCustomCell *cell = // Code 
    cell.button.tag = indexPath.row; 

    // Other cell preparation code 

    return cell; 
} 

然後,在爲bu tton,你可以看到它的標籤是什麼,以確定哪些模型對象對應於:

- (void)didSelectButton:(id)sender { 
    UIButton *button = sender; 
    NSInteger tag = button.tag; 

    YourModelObject *item = self.items[tag]; 

    // Continue with the business logic for what should 
    // happen when that button is pressed. 
} 
+1

標籤效果很好,直到您開始刪除單元格爲止。然後突然,標籤不再對應正確的模型對象。 –

0

如果用自定義按鈕替換附件詳細信息按鈕,則可以調用accessoryButtonTappedForRowWithIndexPath:方法。

例如 - (設置單元時)把這個在的cellForRowAtIndexPath:

UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 125, 24)]; 
[myAccessoryButton setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal]; 
[cell setAccessoryView:myAccessoryButton]; 
[myAccessoryButton release]; 
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

,然後作爲一個單獨的方法:

用於實現該
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *)indexPath 
{ 
    //do something 
} 
+0

我會避免在該方法中創建任何不必要的對象,方法是檢查輔助視圖的存在並僅在「無」時創建它。 –

+0

好的電話,韋恩。不能同意更多! – joeByDesign

0

這個怎麼樣...

- (UITableViewCell *)cellWithSubview:(UIView *)subview { 
    while (subview && ![subview isKindOfClass:[UITableViewCell self]]) subview = subview.superview; 
    return (UITableViewCell *)subview; 
} 

- (IBAction)buttonClicked:(id)sender { 
    UITableViewCell *theCell = [self cellWithSubview:sender]; 
} 
0

你需要調用這樣這個cellForRowAtIndexPath方法。

- (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] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 


    button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(10 , 10, 200, 20); 
    [button addTarget:self action:@selector(passRowValueMethod:) forControlEvents:UIControlEventTouchUpInside]; 
    [button setTag:indexPath.row]; 
    [button setTitle:[NSString stringWithFormat:@"%d", indexPath.row] forState:UIControlStateNormal]; 
    [cell addSubview:button]; 


    return cell; 
} 

然後定義按鈕目標方法,並在此方法中獲取標籤值。

-(void)passRowValueMethod:(UIButton*)sender 
{ 
    UIButton *buttonGet = sender; 
    NSLog(@"%d", buttonGet.tag); 
} 

希望這會對你有幫助。

0

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

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 

CGRect frame = CGRectMake(0.0, 10.0, 24, 24); 
button.frame = frame; 
[button setTag:((indexPath.section & 0xFFFF) << 16) | 
(indexPath.row & 0xFFFF)]; 
[button setImage:[UIImage imageNamed:@"link-buttoni4.png"] forState:UIControlStateNormal]; 
[button setImage:[UIImage imageNamed:@"link-button-onclicki4.png"] forState:UIControlStateHighlighted]; 
[button setSelected:NO]; 

// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet 

[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
button.backgroundColor = [UIColor clearColor]; 
cell.accessoryView = button; 

}

- (無效)checkButtonTapped:(的UIButton *)發件人 {

NSUInteger section = ((sender.tag >> 16) & 0xFFFF); 
NSUInteger row  = (sender.tag & 0xFFFF); 
NSLog(@"Button in section %i on row %i was pressed.", section, row); 

}

0

試試這個,
就可以知道節和行沒有哪一個被點擊在該方法中細胞的:
- (的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}

1>節可以通過
INT sectionno = indexPath.section
2>小區索引可以是由
INT rowno = indexPath.row
然後得到使用就可以得到這樣的表格單元格,

UITableViewCell *cell=[product_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowno inSection:sectionno]]; 
01來獲得