0

異なる画像とアクションを持つカスタムセルを使用して UITableView のボタンを作成する方法を教えてください。私はこのように試しました。辞書の値に従ってボタンの画像とアクションを追加していますが、最初は正常に機能しますが、スクロールするとボタンの画像が変わります。

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


{

    static NSString *CellIdentifier = @"MediaPickerCustomCell";

    MediaPickerCustomCell *customCell = (MediaPickerCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    if(customCell == nil){

    }

    [customCell._titleLabel  setLineBreakMode:UILineBreakModeWordWrap];
    [customCell._titleLabel setMinimumFontSize:FONT_SIZE];
    [customCell._titleLabel setNumberOfLines:0];
    [customCell._titleLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    [customCell._titleLabel setTag:1];



    NSString *text = [eventsarray objectAtIndex:[indexPath row]];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    [customCell._titleLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
    customCell._titleLabel.attributedText=[attributedarrayevents objectAtIndex:indexPath.row];




    CGFloat aHeight = [tableView rectForRowAtIndexPath:indexPath].size.height;

    CGRect frame= customCell.priorview.frame;
    frame.size.height=aHeight;
    [customCell.priorview setFrame:frame];
    //code to setframe of btn register
    CGFloat aHeight1;

    CGRect frame1= customCell._titleLabel.frame;
    aHeight1=frame1.size.height+frame1.origin.y-40;
    CGRect frame2= customCell.addEventBtn.frame;
    frame2.origin.y=aHeight1;
    [customCell.addEventBtn setFrame:frame2];
    [customCell.addEventBtn setHidden:NO];
    if([[[dataEventArray objectAtIndex:indexPath.row]objectForKey:@"mandatory"]isEqualToString:@"1"]){


        [customCell.addEventBtn setImage:[UIImage imageNamed:@"registered.png"] forState:UIControlStateNormal];
        [customCell.addEventBtn setUserInteractionEnabled:NO];
    }
    else if ([[[dataEventArray objectAtIndex:indexPath.row]objectForKey:@"mandatory"]isEqualToString:@"0"]){
        [customCell.addEventBtn addTarget:self action:@selector(addeventToCaendar) forControlEvents:UIControlEventTouchUpInside];

        [customCell.addEventBtn setBackgroundImage:[UIImage imageNamed:@"register_btn.png"] forState:UIControlStateNormal];
        [customCell.addEventBtn setTitle:@"Register" forState:UIControlStateNormal];
        [customCell.addEventBtn setUserInteractionEnabled:YES];

    }


    if([[[dataEventArray objectAtIndex:indexPath.row] objectForKey:@"priority"] isEqualToString:@"1"]){

        customCell.priorview.backgroundColor=[UIColor colorWithRed:255/255.0 green:35/255.0 blue:35/255.0 alpha:1.0];

    }

    else if([[[dataEventArray objectAtIndex:indexPath.row] objectForKey:@"priority"] isEqualToString:@"2"]){

        customCell.priorview.backgroundColor=[UIColor colorWithRed:237/255.0 green:206/255.0 blue:112/255.0 alpha:1.0];

    }

    else{
        customCell.priorview.backgroundColor=[UIColor colorWithRed:124/255.0 green:197/255.0 blue:118/255.0 alpha:1.0];
    }
    UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeCalledEvents:) ];
    customCell.tag=[[[dataEventArray objectAtIndex:indexPath.row] objectForKey:@"id"] intValue];

    gesture.direction = UISwipeGestureRecognizerDirectionRight;
    [customCell addGestureRecognizer:gesture];
    return customCell;

}

}

4

2 に答える 2

0

これは通常、画像の繰り返しを開始したときに発生します。

このコードを試してください:-

if (cell == nil) {
        cell = [[MediaPickerCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    for (id obj in cell.contentView.subviews){
        [obj removeFromSuperview];
    }

これがあなたの助けになることを願っています。そうでない場合はお知らせください..

于 2013-08-26T12:58:42.623 に答える