【问题标题】:Tableview images chaging when scrolling using custom tableview使用自定义表格视图滚动时表格视图图像发生变化
【发布时间】:2013-12-11 09:23:20
【问题描述】:

我有一个 Tableview,我正在使用 customTableview 单元格来显示单元格

在单元格中,当我单击特定按钮时,我有 5 个按钮(评级按钮),按钮的图像必须更改其正常工作,但是当我再次滚动 tableview 时,它们正在更改为正常评级按钮,请参见以下图像为了清楚起见

这是滚动并点击按钮之前的图像

点击评分按钮后,图像会像这样变化

但是当再次滚动单元格时,它会变成第一张图片

请帮帮我

代码:

- (UITableViewCell )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    dataCell = (DataCel)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (dataCell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DataCel" owner:self options:nil]; dataCell = [nib objectAtIndex:0];
    }
    return dataCell;
}

【问题讨论】:

  • 在 cellForRowAtIndexPath 中显示您的代码
  • 您需要记住/存储单元格数据的状态并再次填写!
  • 我在 CellForRowAtIndexPath - (UITableViewCell )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; dataCell = (DataCel)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (dataCell==nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DataCel" owner:self options:nil]; dataCell = [nib objectAtIndex:0]; } 返回数据单元; }

标签: ios iphone objective-c uitableview


【解决方案1】:

您应该保存按钮图像的状态 在你的

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

插入代码以保持更新 例如:

  static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
//create your cell here if it was not created 
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            [cell.textLabel setFont:[UIFont boldSystemFontOfSize:13]];
            [cell.textLabel setTextColor:[UIColor darkGrayColor]];
            [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize:11]];
            [cell.detailTextLabel setTextColor:[UIColor lightGrayColor]];
            cell.detailTextLabel.numberOfLines = 2;
        }

        NSArray *array = [imageArray objectAtIndex:[indexPath row]];
        if([[array objectAtIndex:3] isEqual:@"rate3"])
        {
            //set the code for the rating 3
        }
        else
        {
            //insert another rate depending on your object
        }

【讨论】:

  • 当单击特定速率按钮时,它不会转到 indexpath 处的 didselect 行,因为我是直接单击按钮,所以我如何获取单元格 indexpath
  • 你的单元格里面不是按钮吗?
  • 在 cellForRow 中创建按钮时,为当前行设置按钮标签 button.tag=[indexPath row];在您的接收者中,输入以下内容 - (void)setState:(id)sender { NSLog(@"sender tag %d",[sender tag]); //这将是您单击的按钮的当前行 //您可以使用 NSIndexPath* indexPath =[NSIndexPath indexPathForRow:[sender tag] inSection:0] 检索当前 indexPath; }
【解决方案2】:

这是因为每次滚动UITableView 时,它都会根据您提供的数据刷新其值!所以不保留价值。为了防止每次滚动数据库时发生这种情况,您可以使用此

static NSString *cellIdentifier = @"Identifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}

for(id obj in cell.contentView.subviews)
{
    if([obj isKindOfClass:[UIImageView class]] || [obj isKindOfClass:[UILabel class]])
    {
        [obj removeFromSuperview];
    }
}

这不会让值重叠。

【讨论】:

    【解决方案3】:

    当滚动时,单元格被重新创建,因此您需要将选定的值保存在一个单独的数组中,并将其设置在 cellForRowAtIndexPath 方法中

    【讨论】:

      【解决方案4】:

      发生这种情况是因为每次滚动 tableview 时,它都会重新创建 tableview 单元格。因此,您可以使用数组来重复使用它。

      【讨论】:

        【解决方案5】:
         - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        
        {
            static NSString *cellId = @"cellId";
            UITableViewCell *cell = [categoriesTableview dequeueReusableCellWithIdentifier:cellId];
            if (cell==nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
            }
            for (UIView *subview in [cell.contentView subviews]) {
                [subview removeFromSuperview];
            }
        
        // ui goes here
        
        return cell
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-30
          • 1970-01-01
          相关资源
          最近更新 更多