开发者

favorites button in the tableviewcells

I implemented the following method in my tableviewcontroller for showing favorites with empty star and filled star:

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

}

and I tried to use in this part:

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

        //[(UILa开发者_开发百科bel*) [[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;

}

My problem is that every fifth button in the cells are working as would be the same. If I tap on the button(empty star) in the first cell, the result will be the following: the first button becoming filled-star, so far so good(the expected behaviour), but in the same time the sixth star becoming filled too. Or if I tap on the second one, the seventh star getting filled and vica versa...

I logged the buttons where I tapping , and I realised, that the memory adresses are equals in the case of buttons above mentioned. Sorry about my english, and thanks a lot for every help in advance!


the problem was that you reused the buttons all the time.

I show you a sample working code to structure a customized UITableViewCell correctly... Let's do it step by step.

Create a new class called MyObjTableViewCellForSelection and provide these methods. Adapt it to your needs!

@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

Then the implementation:

    @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

now in your view class you can finally do:

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

So finally that's the clean way of doing it...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜