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

1

尝试改变:

if(customCell == nil){

    }

至:

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

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

于 2013-08-26T10:48:51.793 回答
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 回答