【问题标题】:iPhone dynamic UIButton in UITableViewUITableView中的iPhone动态UIButton
【发布时间】:2011-01-16 13:46:44
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: iphone uitableview memory-leaks uibutton


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,这只是部分工作,因为当我点击/单击按钮时,它不会直观地更新选择状态。我看到它确实是由 senderId 激活的,但没有即时选择/未选择的 uicontrol 状态(因为它是从上一次开始缓存的?)
    • 那么您需要将设置按钮状态的代码放在该块之外。那有意义吗?也许像现在一样重新发布您的代码?
    • 我现在已经修复了,我最大的错误是按钮切换方法中的 [self.tableView reloadData]。我用这样的真正切换替换它: if([sender isSelected]) ... [sender setSelected:NO]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多