Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableView中设置帧大小图像_Ios_Uitableview - Fatal编程技术网

Ios 在UITableView中设置帧大小图像

Ios 在UITableView中设置帧大小图像,ios,uitableview,Ios,Uitableview,我有一个图像大小:CGRectMake(100,70,50,50)insidecellforrowatinexpath。我需要改变帧大小的图像时,细胞是奇数 代码是: if(indexPath.row %2) { CGRectMake(200,70,50,50) } else { CGRectMake(100,70,50,50) } 在第一次加载TableView时,工作正常,但当我反复滚动时,帧大小的图像显示处于错误位置。它变的很有趣,我不知道为什么 你能帮我吗 非常感谢。您已经在U

我有一个图像大小:
CGRectMake(100,70,50,50)
inside
cellforrowatinexpath
。我需要改变帧大小的图像时,细胞是奇数

代码是:

if(indexPath.row %2)
{
  CGRectMake(200,70,50,50)
}
else
{
  CGRectMake(100,70,50,50)
}
在第一次加载
TableView
时,工作正常,但当我反复滚动时,帧大小的图像显示处于错误位置。它变的很有趣,我不知道为什么

你能帮我吗


非常感谢。

您已经在
UIImageView
子视图上设置了框架:

if(indexPath.row %2) {
    cell.yourImageView.frame = CGRectMake(200,70,50,50)
} else {
    cell.yourImageView.frame = CGRectMake(100,70,50,50)
}

你可以按照下面的方法做

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *CellIdentifier = @"Cell";
 UIImageView * imageView;
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

 if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if(indexpath.row%2 == 0)
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 70, 50, 50)];
    else
    {
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 70, 50, 50)];
        [cell.contentView addSubview:imageView];
        imageView.tag=11;
        [imageView release];
    }
}
else
    {
        imageView = (UIImageView*)[cell.contentView viewWithTag:11];
    }
// here are couple of other statements 
    ...............
}

就我个人而言,我会创建一个自定义表格单元格并执行如下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"ImageCell";
     ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
     [cell configureWithRow:indexPath.row];
 }
然后在自定义表格单元格中,为可调整大小的图像视图创建一个IBOutlet,并添加一个方法:

-(void)configureWithRow:(NSInteger)row
{
    if(indexPath.row %2) 
   {
        self.resizableImageView.frame = CGRectMake(200,70,50,50)
   } 
   else 
   {
       cell.resizableImageView.frame = CGRectMake(100,70,50,50)
    }
}    
这有两个作用

  • 您不必检查nil单元格,因为只要标识符与故事板中的标识符匹配,并且您的视图控制器是UITableViewController,就可以保证将单元格返回dequeueReusableCellWithIdentifier

  • 您可以将单元配置的逻辑移到自定义表视图单元,这才是它真正应该所在的位置。视图控制器不应该关心单元格中图像的大小


  • 你没有设置CGRectMake调用的结果帧…@Bruno Koga:我会怎么做?如何使用?我正在使用情节提要,如果我在UITableView中使用子视图,当我滚动UITableView时,imageview已添加到子视图,imageview已被覆盖。谢谢,我正在使用情节提要中的修复单元。您有解决方案吗?不要使用UITableCell中的单元格,即使您反复滚动此代码也会在正确的位置显示UIImageview。我不明白您的意思,在函数configureWithRow中,我没有得到“Cell.resizebleimageView.frame”