简体   繁体   中英

Custom Reusable UITableViewCell

I want to make a custom table cell that handles filling of text fields automatically. My idea is that I'll just pass an object to the cell class and the cell will then automatically fill in the fields. Everything works fine except that no button inside the cell work, they are all unclickable. What am I doing wrong?

Main Screen:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSInteger row = [indexPath row];
        Claim *claim = [statementsArray objectAtIndex:row];
        NSString * strIndentifier;

        strIndentifier = @"StatementDetailsCellIdentifier";

        StatementDetailsCell *cell = (StatementDetailsCell *) [tableView dequeueReusableCellWithIdentifier:strIndentifier];


        cell.hasWarranty = claim.hasWarranty;
        if(cell == nil) 
        {
            [[NSBundle mainBundle] loadNibNamed:@"StatementDetailsCell" owner:self options:nil];
            cell = [statementCell initWithClaim:claim reuseIdentifier:strIndentifier];

            cell.selectionStyle = UITableViewCellSelectionStyleNone;        
         }

           return cell;
    }

Cell.m :

    -(id)initWithClaim:(Claim *)_claim reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
        claim = _claim;

        [self populate];
        return self;
    }

    -(void)populate 
    {
        barcodeLabel.text = claim.barcode;

        NSLog(@"claim is %@", [claim description]);

        if(claim.points == 0 || claim.points == 0.00)
            valueLabel.text = @"Pending";
        else
            valueLabel.text = [NSString stringWithFormat:@"£%.2f", claim.points];

        modelLabel.text = claim.product;
        warrantyLabel.text = claim.warranty.name;
        APIRequest *apiRequest = [[APIRequest alloc] init];
        dateLabel.text = [apiRequest parseDate:claim.date];
        //hasWarranty = claim.hasWarranty;

        double timeS = [apiRequest getUnixTimestamp:claim.date];
        NSDate *now = [NSDate date];
        NSDate *trueDate = [NSDate dateWithTimeIntervalSince1970:timeS];
        double timeDiffrece = [now timeIntervalSinceDate:trueDate];
        double threemonths = 90*24*3600;

        //Warranty Button

        if(claim.hasWarranty)
        {
            UIImage *buttonImage;
            //13-6-2013
            if([claim.warranty.name isEqualToString:@"Pending"])  {
                buttonImage = [UIImage imageNamed:@"imgBtnWarrantyPending.png"];
                pendingHelp.hidden = NO;
            } else {
                pendingHelp.hidden = YES;
                buttonImage = [UIImage imageNamed:@"warranty_claimed.png"];
            }
            [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
            [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];
            [warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
        } 
        else 
        {
            pendingHelp.hidden = YES;
            if(false) {
                [warrantyButton setBackgroundImage:nil forState:UIControlStateNormal];
                [warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
            } else {
                [warrantyButton setTag:claim.ID];
                UIImage *buttonImage = [UIImage imageNamed:@"add_warranty.png"];
                [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
                [warrantyButton setBackgroundImage:nil forState:UIControlStateHighlighted];
                [warrantyButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchDown];
                warrantyButton.userInteractionEnabled = YES;
                warrantyButton.enabled = YES;

            }
        }
    }

Might be your target would be remove from method . Please assign a break point at the strating of populate method and try to show or hide warrantyButton if target assign for check. and double check name of images.

   if(claim.hasWarranty)
    {
        UIImage *buttonImage;
        //13-6-2013
        if([claim.warranty.name isEqualToString:@"Pending"])  {
            buttonImage = [UIImage imageNamed:@"imgBtnWarrantyPending.png"];
            pendingHelp.hidden = NO;
        } else {
            pendingHelp.hidden = YES;
            buttonImage = [UIImage imageNamed:@"warranty_claimed"];
        }
        [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];
        [warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
        [warrantyButton setHidden:YES];
    } 
    else 
    {
       pendingHelp.hidden = YES;

       [warrantyButton setTag:claim.ID];
       UIImage *buttonImage = [UIImage imageNamed:@"add_warranty"];
       [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
       [warrantyButton setBackgroundImage:nil forState:UIControlStateHighlighted];
       [warrantyButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchDown];
       warrantyButton.userInteractionEnabled = YES;
       warrantyButton.enabled = YES;
       [warrantyButton setHidden:NO];

    }

Might be it never goes into else part for assigning target. I hope you understand it better.

I believe you're trying to replicate what the Sensible TableView framework already does. I'd recommend you check it out first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM