tableviewcells中的收藏夹按钮

时间:2011-09-27 22:46:58

标签: iphone xcode uibutton sender

我在tableviewcontroller中实现了以下方法,用于显示带有空星和填充星的收藏夹:

-(IBAction) tapped:(UIButton *)sender
{
    if([sender isSelected])
    {
        //...
        [sender setImage:[UIImage imageNamed:@"fav-empty.png"] forState:UIControlStateNormal];
        [sender setSelected:NO];
    }
    else 
    {
        //... 
        [sender setImage:[UIImage imageNamed:@"fav-filled.png"] forState:UIControlStateNormal];
        [sender setSelected:YES];
        NSLog(@"%@",sender);
    }

}

我尝试在这部分中使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UILabel *recept_neve=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
        [recept_neve setTag:1];
        UIButton *kedvenc_btn=[UIButton buttonWithType:UIButtonTypeCustom];
        [kedvenc_btn setImage:[UIImage imageNamed:@"fav-empty.png"] forState:UIControlStateNormal];//default appereance on the startup
        kedvenc_btn.frame=CGRectMake(200, 20, 50, 50);
        [kedvenc_btn setTag:2];
        [kedvenc_btn addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
        [[cell contentView] addSubview:recept_neve];
        [[cell contentView] addSubview:kedvenc_btn];

        //[(UILabel*) [[cell contentView] viewWithTag:1] setText:[receptek objectAtIndex:indexPath.section*blokk+1]];
        //[(UIButton *) [cell contentView] viewWithTag:2];



    }
    // Configure the cell...
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    [(UILabel*) [[cell contentView] viewWithTag:1] setText:[receptek objectAtIndex:indexPath.section*blokk+1]];

    return cell;

}

我的问题是,单元格中的每五个按钮的工作方式都是一样的。 如果我点击第一个单元格中的按钮(空星),结果将如下:第一个按钮变为填充星形,到目前为止一直很好(预期的行为),但同时第六个星形变为填充太。或者,如果我点击第二个星球,第七个星星会被填满,反之亦然...

我记录了我点击的按钮,我意识到,在上面提到的按钮的情况下,内存地址是相等的。 对我的英语很抱歉,并且提前感谢您的每一次帮助!

1 个答案:

答案 0 :(得分:1)

问题是你一直在重复使用这些按钮。

我向您展示了一个正确构建自定义UITableViewCell的示例工作代码... 让我们一步一步来做。

创建一个名为MyObjTableViewCellForSelection的新类并提供这些方法。根据您的需求进行调整!

@class MyObj;

@interface MyObjTableViewCellForSelection : UITableViewCell
{
    MyObj *myObj;
    UIImageView *imageView;
    UILabel *titleLabel;

}

@property (nonatomic,strong) MyObj *myObj;
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *titleLabel;

@end

然后执行:

    @implementation MyObjTableViewCellForSelection
    @synthesize myObj;
    @synthesize imageView;
    @synthesize titleLabel;


    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {

            imageView = [[UIImageView alloc] initWithFrame: CGRectZero];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [self.contentView addSubview: imageView];

            titleLabel = [[UILabel alloc] initWithFrame: CGRectZero];
            [titleLabel setFont:[UIFont systemFontOfSize:14.0]];
            [titleLabel setTextColor:[UIColor blackColor]];
            [titleLabel setHighlightedTextColor:[UIColor darkGrayColor]];
            [titleLabel setLineBreakMode: UILineBreakModeWordWrap];
            titleLabel.numberOfLines = 2;

            [self.contentView addSubview: titleLabel];

        }
        return self;
    }

    -(void) layoutSubviews {
        [super layoutSubviews];
        [imageView setFrame:CGRectMake(8.0, 10.0, 20.0, 20.0)];
        [titleLabel setFrame:CGRectMake(40.0, 1.0, 250.0, 40.0)]; //two lines

    }

    - (void)setMyObj:(MyObj *)myObjX {
        if (myObjX != myObj) {
            [myObj release];
            [myObjX retain];
            //myObj = myObjX; //only with ARC on
        }

       titleLabel.text = whatever you need accessing myObj
    }

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
      //  selection has to be adapted to your structure
        [super setSelected:selected animated:animated];
        switch (myObj.selectedFlag) {
            case MYOBJ_STATUS_UNSELECTED:
                imageView.image =  [UIImage imageNamed:@"EmptyBullet.png"];
                 break;
            case MYOBJ_STATUS_SELECTED:
                imageView.image =  [UIImage imageNamed:@"GreyBullet.png"];
                break;


            default:
                break;
        }

@end

现在在您的视图类中,您最终可以执行:

- (MyObj *)myObjForIndexPath:(NSIndexPath *)indexPath {
     MyObj* tmpObj = get the corresponding object
     return tmpObj;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyObjTableViewCellForSelection *cell = (MyObjTableViewCellForSelection *)[tableView dequeueReusableCellWithIdentifier:@"cellID"];
    if (cell ==  Nil) {
        cell = [[ECUTableViewCellForSelection alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
    }
    cell.myObj = [self myObjForIndexPath:indexPath]; //the assignment calls setMyObj of CLASS ECUTableViewCellForSelection that you just declared

    return cell;
}

所以最后这是干净利落的方式......