滚动按钮会在uitableviewcell中更改其图像

时间:2013-08-26 10:26:53

标签: iphone objective-c ios6

任何人都可以告诉我如何使用具有不同图像和动作的自定义单元格在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;

}

}

2 个答案:

答案 0 :(得分:1)

尝试更改:

if(customCell == nil){

    }

致:

if(!customCell)
     customCell = [[MediaPickerCustomCell alloc] initWithStyle:nil reuseIdentifier:CellIdentifier];

这将正确地分配单元格以便重用,从而阻止它以错误的方式使单元格出列。对于initWithStyle,您可以尝试各种类型,但如果它是全部自定义的,您可能已经创建了自己的类型,这就是为什么我在示例中将其保留为nil。

答案 1 :(得分:0)

这通常在它开始重复图像时发生。

请尝试以下代码: -

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

希望这会帮助你。如果不让我知道..