2013-01-23 32 views
-1

當我選擇這些UITableViewCells中的按鈕時,該按鈕有時會傳遞錯誤行中的數據。在這個表格視圖中,有10個部分,每個部分都有一個部分標題和只有1行。行高佔據了屏幕的大部分,因爲其中有6個按鈕。每選中一個UIButton時,都會打開一個新的viewcontroller,其中放大了按鈕圖像的視圖(刪除此代碼以使其更具可讀性)。UITableViewCell中的UIButton操作選擇了錯誤的行

下面是問題:每個按鈕成功推動一個模式viewcontroller;然而,當我向下滾動時,按鈕有時會傳遞來自其下方部分的相同按鈕的數據(例如,點擊時第4節中的button1可能會傳遞第5節中button1的數據)。同樣,當我向後滾動時,按鈕有時會傳遞來自其上方相同按鈕的數據(例如,點擊時,第7節中的button5可能會傳遞第6節中button5的數據)。

我想這個問題是因爲iPhone拉最近加載的單元格(當調用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]; 
} 

// dataDict pulled from plist  
NSDictionary *button1Dict = [dataDict objectForKey:@"button1"]; 
NSDictionary *button2Dict = [dataDict objectForKey:@"button2"]; 
NSDictionary *button3Dict = [dataDict objectForKey:@"button3"]; 
NSDictionary *button4Dict = [dataDict objectForKey:@"button4"]; 
NSDictionary *button5Dict = [dataDict objectForKey:@"button5"]; 
NSDictionary *button6Dict = [dataDict objectForKey:@"button6"]; 

// selectedCatTag and selectedItemTag for each button are alloc and init in viewDidLoad 
button1SelectedCatTag = (NSUInteger *)[[button1Dict objectForKey:@"selectedCatTag"] integerValue]; 
button2SelectedCatTag = (NSUInteger *)[[button2Dict objectForKey:@"selectedCatTag"] integerValue]; 
button3SelectedCatTag = (NSUInteger *)[[button3Dict objectForKey:@"selectedCatTag"] integerValue]; 
button4SelectedCatTag = (NSUInteger *)[[button4Dict objectForKey:@"selectedCatTag"] integerValue]; 
button5SelectedCatTag = (NSUInteger *)[[button5Dict objectForKey:@"selectedCatTag"] integerValue]; 
button6SelectedCatTag = (NSUInteger *)[[button6Dict objectForKey:@"selectedCatTag"] integerValue]; 

button1SelectedItemTag = (NSUInteger *)[[button1Dict objectForKey:@"selectedItemTag"] integerValue]; 
button2SelectedItemTag = (NSUInteger *)[[button2Dict objectForKey:@"selectedItemTag"] integerValue]; 
button3SelectedItemTag = (NSUInteger *)[[button3Dict objectForKey:@"selectedItemTag"] integerValue]; 
button4SelectedItemTag = (NSUInteger *)[[button4Dict objectForKey:@"selectedItemTag"] integerValue]; 
button5SelectedItemTag = (NSUInteger *)[[button5Dict objectForKey:@"selectedItemTag"] integerValue]; 
button6SelectedItemTag = (NSUInteger *)[[button6Dict objectForKey:@"selectedItemTag"] integerValue]; 

UIButton *button1 = (UIButton *)[cell viewWithTag:1]; 
UIButton *button2 = (UIButton *)[cell viewWithTag:2]; 
UIButton *button3 = (UIButton *)[cell viewWithTag:3]; 
UIButton *button4 = (UIButton *)[cell viewWithTag:4]; 
UIButton *button5 = (UIButton *)[cell viewWithTag:5]; 
UIButton *button6 = (UIButton *)[cell viewWithTag:6]; 

CGFloat kImageSquareSideLength = 100.0; 

if (button1Dict) { 
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button1SelectedCatTag]; 
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button1SelectedItemTag]; 
    [button1 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal]; 
    [button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside]; 
} 
if (button2Dict) { 
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button2SelectedCatTag]; 
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button2SelectedItemTag]; 
    [button2 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal]; 
    [button2 addTarget:self action:@selector(button2Pressed:) forControlEvents:UIControlEventTouchUpInside]; 
} 
if (button3Dict) { 
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button3SelectedCatTag]; 
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button3SelectedItemTag]; 
    [button3 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal]; 
    [button3 addTarget:self action:@selector(button3Pressed:) forControlEvents:UIControlEventTouchUpInside]; 
} 
if (button4Dict) { 
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button4SelectedCatTag]; 
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button4SelectedItemTag]; 
    [button4 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal]; 
    [button4 addTarget:self action:@selector(button4Pressed:) forControlEvents:UIControlEventTouchUpInside]; 
} 
if (button5Dict) { 
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button5SelectedCatTag]; 
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button5SelectedItemTag]; 
    [button5 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal]; 
    [button5 addTarget:self action:@selector(button5Pressed:) forControlEvents:UIControlEventTouchUpInside]; 
} 

if (button6Dict) { 
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button6SelectedCatTag]; 
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button6SelectedItemTag]; 
    [button6 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal]; 
    [button6 addTarget:self action:@selector(button6Pressed:) forControlEvents:UIControlEventTouchUpInside]; 
} 

return cell; 

} 

//all other button actions are identical to button1Pressed 
- (IBAction)button1Pressed:(id)sender 
{ 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
SelectedItemViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:@"SelectedItemViewController"]; 

destVC.selectedOutfitDict = @"DiscoverOutfits"; 
destVC.selectedCatTag = (int *)button1SelectedCatTag; 
destVC.selectedItemTag = (int *)button1SelectedItemTag; 

[self presentViewController:destVC animated:NO completion:nil]; 
} 
+0

添加代碼以傳遞項目數據。我認爲這就是問題出在哪裏 –

+0

http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview?rq=1 – junaidsidhu

+0

所以你想呈現相同的viewcontroller爲每個按鈕的動作? –

回答

1

是的,你可以從哪個按鈕錄製特定的細胞。在IBAction爲方法寫,你會在indexPath對象中獲取特定的細胞錄音按鈕

UIView *senderButton = (UIView*) sender; 
     NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]]; 

希望這將有助於

+0

我刪除了我以前的評論,以避免將來的讀者混淆。我意識到我的錯誤,並且你的答案最接近它 - 我正在爲cellForRowAtIndexPath中的每個按鈕設置數據!當然,傳遞的數據是最近加載的單元格。顯而易見的解決方案是在按下單元格時爲每個按鈕設置數據。大家好,感謝您的耐心和幫助。 – user1978374

1

有時它標註的問題。

更改標籤是這樣的:

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

    UIButton *button1 = [[UIButton alloc] init]; 
    button1.tag = indexPath.section + indexPath.row + 8956;// 8956 is some random weird  number 
    [cell.contentView addSubview:button1]; 
    [button1 release]; 

// same code for other buttons 

} 

UIButton *button1 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row + 8956]; 
UIButton *button2 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956]; 
UIButton *button3 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956]; 
UIButton *button4 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956]; 
UIButton *button5 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956]; 
UIButton *button6 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956]; 

工作?

+0

@Neelz - 這個解決方案不起作用,因爲每個單元格內有6個按鈕(所以在將數據傳遞到下一個視圖控制器時,這些標籤無法區分不同的按鈕)。解決方法是在buttonPressed方法中設置按鈕數據 - 一個相當愚蠢的錯誤 - 您對我的幫助表示衷心的感謝。 – user1978374

+0

@ user1978374:你知道它的一些標籤是相同的...然後我們應該製作標籤(一些數學),以便它們不會重複。 Thx –

+0

@ Neelz這不會工作 –

相關問題