【问题标题】:changing UIButton image background in Table view cell for particular row base on som condition根据某些条件更改特定行的表格视图单元格中的 UIButton 图像背景
【发布时间】:2016-03-17 05:57:48
【问题描述】:

我的应用中有一个自定义表格视图,每行都有按钮。 当该单元格的标志为 i'1' 时,我想更改按钮背景图像(检查代码中的 if 语句。)。
它正在成功执行 if 语句。
只是按钮的背景图像没有改变。
这是我的代码

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

cell= [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]autorelease];


if(tableView == tablePatchFilter)
{
    cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
}
else
{
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UILabel *typeLabel = [[[UILabel alloc]init]autorelease];
    typeLabel.backgroundColor = [UIColor clearColor];
    typeLabel.text = [displayItemsType objectAtIndex:indexPath.row];
    typeLabel.textAlignment = NSTextAlignmentLeft;
    typeLabel.font=[UIFont fontWithName:MyFont size:17.f];
    typeLabel.frame = CGRectMake(300,2.0f,250,50);
    [cell addSubview:typeLabel];


    UILabel *typeLabel1 = [[[UILabel alloc]init]autorelease];
    typeLabel1.backgroundColor = [UIColor clearColor];
    typeLabel1.text = [displayItemPatch objectAtIndex:indexPath.row];
    typeLabel1.textAlignment = NSTextAlignmentLeft;
    typeLabel1.font=[UIFont fontWithName:MyFont size:17.f];
    typeLabel1.frame = CGRectMake(550,2.0f,250,50);
    [cell addSubview:typeLabel1];


    UILabel *typeLabel2 = [[[UILabel alloc]init]autorelease];
    typeLabel2.backgroundColor = [UIColor clearColor];
    typeLabel2.font=[UIFont fontWithName:MyFont size:17.f];
    typeLabel2.text = [displayItemclass objectAtIndex:indexPath.row];
    typeLabel2.textAlignment = NSTextAlignmentLeft;
    typeLabel2.frame = CGRectMake(730,2.0f,250,50);
    [cell addSubview:typeLabel2];


    int selectedSegment = mainSegment.selectedSegmentIndex;



    if(selectedSegment == 0)
    {





        for(int i = 0;i<[displayFlag count];i++)


        { UIButton  *btn = [UIButton buttonWithType:UIButtonTypeCustom];


            btn.tag=indexPath.row;

            [btn addTarget:self
                    action:@selector(takeSignature:)
          forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:btn];


            NSMutableString *pathhh= [NSMutableString stringWithFormat:@"%@",[displayFlag objectAtIndex:i]];

            NSLog(@"XXXXXXXXXXXXXXXXXXXXXXXXXX%@",pathhh);



            if([pathhh isEqualToString:@"1"])
            {

           NSLog(@"in IFFFFFFF%@",pathhh);


                  btn.frame = CGRectMake(850,1, 50, 50);
                [btn setBackgroundImage:[UIImage imageNamed:@"signature.png"]
                               forState:UIControlStateNormal];

            }

         else
            {   
              NSLog(@"in ELSSSSSSSSSS %@",pathhh);


                  btn.frame = CGRectMake(850,1, 50, 50);

                [btn setBackgroundImage:[UIImage imageNamed:@"add.png"]
                               forState:UIControlStateNormal];

            }

        }


        /*

       UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(850,1, 50, 50);
        [btn setBackgroundImage:[UIImage imageNamed:@"signature.png"]
                       forState:UIControlStateNormal];
        [btn addTarget:self
                action:@selector(takeSignature:)
      forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btn];
         */

        }










}
cell.textLabel.font=[UIFont fontWithName:MyFont size:17.f];
return cell;
}

【问题讨论】:

  • 你遇到的问题,它显示蓝色,你看到了什么
  • 好的,问题是。两个按钮都重叠了。相反,我想要如果该行的标志是一个我想将图像更改为 if 语句中指定的其他图像

标签: ios objective-c uitableview uibutton


【解决方案1】:

在您的实现中,每次创建新单元格时,从“dequeueReusableCellWithIdentifier”获取单元格后,您必须检查单元格是否为零,而不是仅使用“alloc-init”创建新单元格。

特定 senireo 的更好实现是采用自定义 UITableViewCell。

您应该遵循以下策略。

  1. 在自定义单元格中为标签 1、2 和 3 创建出口,然后在 cellForRowatIndexPath 中设置测试。
  2. 在自定义单元格类中创建 NSMutableArray 的属性。
  3. 从“dequeueReusableCellWithIdentifier”获取单元格后,检查单元格是否为 nil,而不是仅创建按钮并将所有按钮添加到步骤 2 中创建的 NSMutableArray 中。
  4. 从 NSMutableArray 中获取按钮并应用设置图像的条件。

请看下面的代码:

    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(tableView == tablePatchFilter)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
        return cell;

    } else {
        CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"cell"];

        if (!cell) {

            cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

            int selectedSegment = mainSegment.selectedSegmentIndex;

            if(selectedSegment == 0) {
                UIButton  *btn = [UIButton buttonWithType:UIButtonTypeCustom];

                btn.tag=indexPath.row;

                [btn addTarget:self action:@selector(takeSignature:) forControlEvents:UIControlEventTouchUpInside];
                [cell.buttonArray addObject:btn];
            }
        }

        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

        cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.label1.text = [displayItemsType objectAtIndex:indexPath.row];
        cell.label2.text = [displayItemPatch objectAtIndex:indexPath.row];
        cell.label3.text = [displayItemclass objectAtIndex:indexPath.row];

        for (UIButton *btn in cell.buttonArray) {
            NSMutableString *pathhh= [NSMutableString stringWithFormat:@"%@",[displayFlag objectAtIndex:i]];

            if([pathhh isEqualToString:@"1"]) {

                btn.frame = CGRectMake(850,1, 50, 50);
                [btn setBackgroundImage:[UIImage imageNamed:@"signature.png"] forState:UIControlStateNormal];

            } else {
                btn.frame = CGRectMake(850,1, 50, 50);
                [btn setBackgroundImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
            }
        }

        return cell;
    }

}

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 2012-01-29
    • 2023-03-26
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多