Search code examples
iphoneiosios4didselectrowatindexpathnsindexpath

iOS Dynamic Button tagging in UITableViewCell does not detect correct row in table


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;
    }
);

}

My code for the 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:

Thanks much!


Solution

  • 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 :)