-1

これらのボタンを選択するUITableViewCellsと、ボタンが間違った行からデータを渡すことがあります。このテーブル ビューには 10 個のセクションがあり、それぞれにセクション ヘッダーがあり、1 行しかありません。画面内に 6 つのボタンがあるため、行の高さが画面の大部分を占めます。各UIButtonを選択すると、ボタンの画像が拡大された新しい が開きviewcontrollerます (ここでは読みやすくするためにこのコードを削除しました)。

問題は次のとおりです。各ボタンはモーダルを正常にプッシュしviewcontrollerます。ただし、下にスクロールすると、ボタンがその下のセクションから同じボタンのデータを渡すことがあります (たとえば、セクション 4 のボタン 1 をクリックすると、セクション 5 のボタン 1 のデータが渡される場合があります)。同様に、上にスクロールすると、ボタンがその上のセクションから同じボタンのデータを渡すことがあります (たとえば、セクション 7 のボタン 5 をクリックすると、セクション 6 のボタン 5 のデータが渡される場合があります)。

この問題は、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];
}
4

2 に答える 2

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];

働く??

于 2013-01-23T06:42:54.063 に答える
1

はい、ボタンがテープされている特定のセルを取得できます。IBAction メソッドに書き込むと、indexPath オブジェクトに特定のセル テープ ボタンが表示されます。

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

これが役立つことを願っています

于 2013-01-23T06:12:42.410 に答える