UITableView中的iPhone动态UIButton [英] iPhone dynamic UIButton in UITableView

查看:79
本文介绍了UITableView中的iPhone动态UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为UITableView中的每个单元格行创建一个按钮.该按钮用作将所选行添加为NSUserDefaults中的收藏夹"的开关.我的问题是,每当我按下此按钮时,都会在旧按钮的上方绘制一个新按钮.如何正确释放/重用它? 这是我的cellForRowAtIndexPath方法的样子:

I'm creating a button for each cell row in a UITableView. The button acts as switch to add the selected row as 'favorite' in NSUserDefaults. My problem is that whenever I press this button, a new one get drawn on top of the old one. How do I release/reuse it right? This is what my cellForRowAtIndexPath method looks like:

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

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

    } 

    UIImage *favOn = [UIImage imageNamed:@"favOn.png"];
    UIImage *favOff = [UIImage imageNamed:@"favOff.png"];                   
    UIButton *favButton = [[UIButton alloc] initWithFrame:CGRectMake(230, 0, 54, 54)];


    favButton.tag = viewTag;

    [favButton setImage:favOff forState:UIControlStateNormal];
    [favButton setImage:favOn forState:UIControlStateSelected];

    if([self getFavState:viewTag]) {

        [favButton setSelected:YES];
    }
    else {
        [favButton setSelected:NO];
    }

    [favButton addTarget:self action:@selector(favButtonSwitch:) forControlEvents:UIControlEventTouchUpInside];
    [favButton setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:favButton];
    [favButton release];

    return cell;
}

此外,我使用三种不同的方法来选择按钮:

Further I use three different methods for selections of the buttons:

- (void) setFavState:(BOOL)state withId:(NSString *)uid {

    NSUserDefaults *savedData = [NSUserDefaults standardUserDefaults];
    [savedData setBool:state forKey:uid];   
}

- (BOOL) getFavState:(NSString *)uid {

    NSUserDefaults *savedData = [NSUserDefaults standardUserDefaults];
    return [savedData boolForKey:uid];
}

- (void) favButtonSwitch:(id) sender {
    NSInteger senderId = [sender tag];
    NSString *senderString = [NSString stringWithFormat:@"%i", senderId];

    NSLog(@"sender:%i",senderId);

    [self getFavState:senderString];

    if([self getFavState:senderString]) {

        [self setFavState:NO withId:senderString];
    }
    else {
        [self setFavState:YES withId:senderString];
    }

    [self.tableView reloadData];
}

推荐答案

您似乎每次都在创建一个带有新图像的新按钮,即使该按钮是从缓存中检索到的.将用于创建带有图像按钮的代码移到首次创建单元格的代码中.

You seem to be creating a new button with new images every time, even if it was retrieved from the cache. Move the code for creating a button with images into the code that creates a cell for the first time.

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // Move all the image and button creation in here...
} 

这篇关于UITableView中的iPhone动态UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆