UIButton 被添加到 tableView 几次

发布于 2024-11-09 16:13:21 字数 4112 浏览 0 评论 0原文

我有一个名为 Checkbox 的 UIButton 子类,当调用 cellForRowAtIndex 方法时,我将其添加到单元格内容视图中。

但我有一个问题。该按钮会多次添加到单元格中。我怎样才能防止这种情况发生?

这是我的代码:

RootViewController:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *lab, *dlabl, *cornerLabel;
    CheckBox *btn =  (CheckBox *)[_checkboxArray objectAtIndex:indexPath.row];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        //I added this code:
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 3; // 0 means no max.


        UIImageView* img = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient7.png"]] autorelease];
        [cell setBackgroundView:img];

        lab = [[[UILabel alloc] initWithFrame:CGRectMake(40, 18, cell.contentView.frame.size.width-15, 22)] autorelease];
        [lab setBackgroundColor:[UIColor clearColor]];
        [lab setTextColor:[UIColor whiteColor]];
        [lab setAdjustsFontSizeToFitWidth:YES];
        [lab setTextAlignment:UITextAlignmentLeft];
        [lab setTag:kTEXT_LABEL_TAG];
        [cell.contentView addSubview:lab];

        dlabl = [[[UILabel alloc] initWithFrame:CGRectMake(5, 54, cell.contentView.frame.size.width- 1, 22)] autorelease];
        [dlabl setTextColor:[UIColor colorWithRed:1.0 green:0.80 blue:0.0 alpha:1.0]];
        [dlabl setBackgroundColor:[UIColor clearColor]];
       // [dlabl setAdjustsFontSizeToFitWidth:YES];
        [dlabl setTextAlignment:UITextAlignmentLeft];
        [dlabl setTag:kDETAIL_TEXT_LABEL_TAG];
        [dlabl setFont:[UIFont systemFontOfSize:[lab font].pointSize - 3]];
        [cell.contentView addSubview:dlabl];

        cornerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 40, 19, 40, 20)] autorelease];
        [cornerLabel setTextColor:[UIColor whiteColor]];
        //[cornerLabel setFont:[UIFont systemFontOfSize:12]];
        [cornerLabel setAdjustsFontSizeToFitWidth:YES];
        [cornerLabel setBackgroundColor:[UIColor clearColor]];
        [cornerLabel setTextAlignment:UITextAlignmentCenter];
        [cornerLabel setTag:kCORNER_TEXT_LABEL_TAG];
        [cell.contentView addSubview:cornerLabel];
        [cornerLabel setAdjustsFontSizeToFitWidth:YES];
    }
    else {
        lab = (UILabel *)[[cell contentView] viewWithTag:kTEXT_LABEL_TAG];
        dlabl = (UILabel *)[[cell contentView] viewWithTag:kDETAIL_TEXT_LABEL_TAG];
        cornerLabel = (UILabel *)[[cell contentView] viewWithTag:kCORNER_TEXT_LABEL_TAG];
    }
    NSDictionary *dict = [_cellTextArray objectAtIndex:indexPath.row];
    lab.text = [dict objectForKey:@"textLabel"];
    dlabl.text = [dict objectForKey:@"detailTextLabel"];
    cornerLabel.text = [dict objectForKey:@"cornerLabel"];
    [cell.contentView addSubview:btn];
    return cell;
}

CheckBox.m

- (void)checkImages {
    NSUInteger tag = [self tag];
    BOOL val = [[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%i", tag]];
    if (val == YES) {
        [self setImage:[UIImage imageNamed:@"checkbox-pressed.png"] forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:[NSString stringWithFormat:@"%i", tag]];
    }
    else if (val == NO) {
        [self setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"%i", tag]];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([[[[event allTouches] anyObject] view] tag] == [self tag]) {
        [self checkImages];
    }
}

I have a UIButton subclass called Checkbox, and I add it to the cells content view when the cellForRowAtIndex method is called.

I have a problem though. The button is added to the cell a few times. How can I prevent this?

Here is my code:

RootViewController:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *lab, *dlabl, *cornerLabel;
    CheckBox *btn =  (CheckBox *)[_checkboxArray objectAtIndex:indexPath.row];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        //I added this code:
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 3; // 0 means no max.


        UIImageView* img = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient7.png"]] autorelease];
        [cell setBackgroundView:img];

        lab = [[[UILabel alloc] initWithFrame:CGRectMake(40, 18, cell.contentView.frame.size.width-15, 22)] autorelease];
        [lab setBackgroundColor:[UIColor clearColor]];
        [lab setTextColor:[UIColor whiteColor]];
        [lab setAdjustsFontSizeToFitWidth:YES];
        [lab setTextAlignment:UITextAlignmentLeft];
        [lab setTag:kTEXT_LABEL_TAG];
        [cell.contentView addSubview:lab];

        dlabl = [[[UILabel alloc] initWithFrame:CGRectMake(5, 54, cell.contentView.frame.size.width- 1, 22)] autorelease];
        [dlabl setTextColor:[UIColor colorWithRed:1.0 green:0.80 blue:0.0 alpha:1.0]];
        [dlabl setBackgroundColor:[UIColor clearColor]];
       // [dlabl setAdjustsFontSizeToFitWidth:YES];
        [dlabl setTextAlignment:UITextAlignmentLeft];
        [dlabl setTag:kDETAIL_TEXT_LABEL_TAG];
        [dlabl setFont:[UIFont systemFontOfSize:[lab font].pointSize - 3]];
        [cell.contentView addSubview:dlabl];

        cornerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 40, 19, 40, 20)] autorelease];
        [cornerLabel setTextColor:[UIColor whiteColor]];
        //[cornerLabel setFont:[UIFont systemFontOfSize:12]];
        [cornerLabel setAdjustsFontSizeToFitWidth:YES];
        [cornerLabel setBackgroundColor:[UIColor clearColor]];
        [cornerLabel setTextAlignment:UITextAlignmentCenter];
        [cornerLabel setTag:kCORNER_TEXT_LABEL_TAG];
        [cell.contentView addSubview:cornerLabel];
        [cornerLabel setAdjustsFontSizeToFitWidth:YES];
    }
    else {
        lab = (UILabel *)[[cell contentView] viewWithTag:kTEXT_LABEL_TAG];
        dlabl = (UILabel *)[[cell contentView] viewWithTag:kDETAIL_TEXT_LABEL_TAG];
        cornerLabel = (UILabel *)[[cell contentView] viewWithTag:kCORNER_TEXT_LABEL_TAG];
    }
    NSDictionary *dict = [_cellTextArray objectAtIndex:indexPath.row];
    lab.text = [dict objectForKey:@"textLabel"];
    dlabl.text = [dict objectForKey:@"detailTextLabel"];
    cornerLabel.text = [dict objectForKey:@"cornerLabel"];
    [cell.contentView addSubview:btn];
    return cell;
}

CheckBox.m

- (void)checkImages {
    NSUInteger tag = [self tag];
    BOOL val = [[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%i", tag]];
    if (val == YES) {
        [self setImage:[UIImage imageNamed:@"checkbox-pressed.png"] forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:[NSString stringWithFormat:@"%i", tag]];
    }
    else if (val == NO) {
        [self setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"%i", tag]];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([[[[event allTouches] anyObject] view] tag] == [self tag]) {
        [self checkImages];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2024-11-16 16:13:21

仅在创建单元格时(即,使可重用单元格出队失败时)添加按钮。

Only add your button, when the cell is created, i.e. when dequeueing a reusable cell fails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文