自定义可重用UITableViewCell

时间:2013-07-09 08:58:18

标签: ios objective-c uitableview

我想制作一个自动填充文本字段的自定义表格单元格。我的想法是,我只是将一个对象传递给单元格类,然后单元格将自动填入字段。一切正常,除了单元格内没有按钮工作,它们都是无法点击的。我做错了什么?

主屏幕:

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

            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您的目标可能会从方法中移除。请在populate方法的指示中指定一个断点,如果目标分配用于检查,请尝试显示或隐藏warrantyButton。并仔细检查图像名称。

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

    }

可能永远不会进入分配目标的其他部分。我希望你能更好地理解它。

答案 1 :(得分:0)

我相信你正试图复制Sensible TableView框架已经做的事情。我建议你先检查一下。