El etiquetado de botones dinámicos de iOS en UITableViewCell no detecta la fila correcta en la tabla

I'm experiencing a major problem now as my renewal button does not detect the row that is being selected at all because of the recycling! Tried tagging the button but it doesn't detect the correct button either. Please help! I've been stuck on this for several days. If not, feel free to recommend other better solutions other than tagging. I parse in sections and rows via NSDictionary which looks like this:

"15/08/2012" =     (
            {
        BIBNo = 290408;
        date = "15/08/2012";
        itemSequence = 000010;
        name = "Sams teach yourself iPhone application development in 24 hours / John Ray.";
        noOfRenewal = 3;
        number = 000290408;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    }
);
"16/08/2012" =     (
            {
        BIBNo = 187883;
        date = "16/08/2012";
        itemSequence = 000010;
        name = "Positive thinking / Susan Quilliam.";
        noOfRenewal = 3;
        number = 000187899;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    },
            {
        BIBNo = 161816;
        date = "16/08/2012";
        itemSequence = 000010;
        name = "Malaysian Q & As / compiled by Survey & Interview Department ; illustrations by Exodus.";
        noOfRenewal = 1;
        number = 000161817;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    }
);
"17/08/2012" =     (
            {
        BIBNo = 187882;
        date = "17/08/2012";
        itemSequence = 000010;
        name = "Increasing confidence / Philippa Davies.";
        noOfRenewal = 1;
        number = 000187907;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    },
            {
        BIBNo = 244441;
        date = "17/08/2012";
        itemSequence = 000010;
        name = "Coach : lessons on the game of life / Michael Lewis.";
        noOfRenewal = 2;
        number = 000244441;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    },
            {
        BIBNo = 291054;
        date = "17/08/2012";
        itemSequence = 000010;
        name = "iPhone application development for IOS 4 / Duncan Campbell.";
        noOfRenewal = 3;
        number = 000291054;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    }
);

}

Mi código para el cellForRowAtIndexPath:

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

static NSString *CellIdentifier = @"Cell";
UserCustomCell *cell = (UserCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);

if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"UserCustomCell" owner:self options:nil];
    cell = userCustomCell;
    self.userCustomCell = nil;
}

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);
    cell.renewButton.frame = CGRectMake(600, 14, 68, 24);
}
[cell.renewButton useBlackActionSheetStyle];

dataSource = [[NSMutableDictionary alloc] init]; // This would need to be an ivar

for (NSDictionary *rawItem in myArray) {
    NSString *date = [rawItem objectForKey:@"date"]; // Store in the dictionary using the data as the key

    NSMutableArray *section = [dataSource objectForKey:date]; // Grab the section that corresponds to the date

    if (!section) { // If there is no section then create one and add it to the dataSource
        section = [[NSMutableArray alloc] init];
        [dataSource setObject:section forKey:date];
    }
    [section addObject:rawItem]; // add your object
}
self.dataSource = dataSource;
NSLog(@"Data Source Dictionary: %@", dataSource); 

NSArray *sections =[[dataSource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *sectionTitle = [sections objectAtIndex:indexPath.section];

NSArray *items = [dataSource objectForKey:sectionTitle];

NSDictionary *dict = [items objectAtIndex:indexPath.row];
cell.bookTitle.text = [dict objectForKey:@"name"];
cell.detail.text = [NSString stringWithFormat:@"Due Date: %@ Due Time: %@", 
                    [dict objectForKey:@"date"], [dict objectForKey:@"time"]];


cell.renewButton.tag = indexPath.row;
return cell;   

}

How it looks like now:

¡Muchas gracias!

preguntado el 29 de julio de 12 a las 11:07

No crees tu dataSource in this method; init and fill it una vez in viewDidLoad or viewWillAppear...wherever you have the data available. Other than that, what is the code like that handles the Renew button? -

@Phillip It will fire the method in which it would check for internet connection, and would check the button.tag for the number of renewal (as max renewal is three times) NSString *noRenewal = [[myArray objectAtIndex:button.tag] objectForKey:@"noOfRenewal"]; -

You should be using the same collection for looking up the renewal that you use for populating the cell. So, again, I suggest creating your dataSource once and then using section/row to get information from it for both purposes rather than myArray. Otherwise, your tag value needs to be the index into the array rather than the row. -

1 Respuestas

I've resolved the issue by adding index into my array which was recommended by @Phillip which looks like this:

for (int a = 0; a < myArray.count; a++){

    NSMutableDictionary *mDict = [myArray objectAtIndex:a];
    NSNumber *myIndex = [NSNumber numberWithInt:a];
    [mDict setValue:myIndex forKey:@"index"];
    NSLog(@"added index: %@", myArray);
}
self.myArray = myArray;

before i set the tag in my button so that it would detect the correct item.

 NSString *index = [dict objectForKey:@"index"];
NSInteger index_tag = (@"%d", [index intValue]);
NSLog(@"%d", index_tag);
cell.renewButton.tag = index_tag;

Hope it may help anyone with the same problem as me :)

Respondido 30 Jul 12, 08:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.