Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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 向UITableViewCell动态添加子视图_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 向UITableViewCell动态添加子视图

Ios 向UITableViewCell动态添加子视图,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试向UITableViewCell添加一些子视图。子视图的数量基于我的数据。向下滚动时,子视图将消失,不再显示。将它们添加到NIB是没有选择的,因为我现在只知道运行时子视图的数量,并且每个单元的子视图都不同 在运行时,将未知数量的子视图添加到UITableViewCell的正确方法是什么 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat

我正在尝试向UITableViewCell添加一些子视图。子视图的数量基于我的数据。向下滚动时,子视图将消失,不再显示。将它们添加到NIB是没有选择的,因为我现在只知道运行时子视图的数量,并且每个单元的子视图都不同

在运行时,将未知数量的子视图添加到UITableViewCell的正确方法是什么

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

    DetailCellTableViewCell *cell = (DetailCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DetailCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];


        NSInteger count = [self getMaxSubviews];
        NSInteger y=100;

        for (int i=0; i<count;i++)
        {
            UITextField *dataS = [[UITextField alloc] init];

            dataS.frame=CGRectMake(277, y, 60, 17);

            y=y+17;

            dataS.tag=i+1337;
            dataS.backgroundColor=[UIColor redColor];

            [cell addSubview:dataS];
        }
    }


    if (!useOrigCellFromNib) // Here I can use the original Nib created by IB
    {

        NSString *data = @"Some String";

        [cell.data setText:data];


    }
    else // Use added subviews!
    {

        for (int i=0;i<arrS.count;i++)
        {
            NSManagedObject *s = [arrS objectAtIndex:i];


            UITextView *dataS =[cell viewWithTag:i+1337];

            dataS.text=[NSString stringWithFormat:@"%ld foo", (long)i];
            [cell.data setHidden:YES];

        }

    }

    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*simpleTableIdentifier=@“DetailCell”;
DetailCellTableViewCell*单元格=(DetailCellTableViewCell*)[tableView出列重用CellWithIdentifier:simpleTableIdentifier];
如果(单元格==nil)
{
NSArray*nib=[[NSBundle mainBundle]loadNibNamed:@“DetailCell”所有者:自选项:nil];
单元格=[nib objectAtIndex:0];
NSInteger count=[self getMaxSubviews];
NSInteger y=100;

对于(int i=0;i1)您应该重用单元格,请调用tableView.dequeueReusableCellWithIdentifier(“CellId”)

2获得重用单元后,应删除以前添加的所有自定义子视图

3之后,可以添加新的子视图

关于“我向下滚动子视图将消失且不再显示” 我以前没有看到任何“cell”变量

if (cell == nil)

因此,您可能不会在此处粘贴重用代码,在这种情况下,滚动后的单元格将不会为零,if(cell==nil)下的代码将不会被调用…

1您应该重用单元格,请调用tableView.dequeueReusableCellWithIdentifier(“CellId”)

2获得重用单元后,应删除以前添加的所有自定义子视图

3之后,可以添加新的子视图

关于“我向下滚动子视图将消失且不再显示” 我以前没有看到任何“cell”变量

if (cell == nil)

因此,您可能不会在此处粘贴重用代码,在这种情况下,滚动后的单元格将不会为零,if(cell==nil)下的代码将不会被调用…

就像Igor在重用单元格时提到的那样,您必须删除之前添加的内容并重新创建子视图。 可能您不能使用“loadFromNib”和子类“UITableViewCell”类在那里创建单元格

这是swift中的一个示例,但ObjC的逻辑也一样

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let stuffArray = array[indexPath.row]

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell")
    if cell == nil {
        cell = MyCustomCell(initWithDaraArray:stuffArray) // create cell based on array data dynamically

    } else { // even if you have cell you need to refresh it for new data
        cell.refreshDataForDataInArray(stuffArray) // here remove all subviews and create new ones
    }

    return cell
}
单元高度可通过以下方式进行调整:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let stuffArray = array[indexPath.row]
    return calculatedHeight(stuffArray)
}

就像Igor在重用单元时提到的那样,您必须删除以前添加的内容并重新创建子视图。 可能您不能使用“loadFromNib”和子类“UITableViewCell”类在那里创建单元格

这是swift中的一个示例,但ObjC的逻辑也一样

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let stuffArray = array[indexPath.row]

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell")
    if cell == nil {
        cell = MyCustomCell(initWithDaraArray:stuffArray) // create cell based on array data dynamically

    } else { // even if you have cell you need to refresh it for new data
        cell.refreshDataForDataInArray(stuffArray) // here remove all subviews and create new ones
    }

    return cell
}
单元高度可通过以下方式进行调整:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let stuffArray = array[indexPath.row]
    return calculatedHeight(stuffArray)
}

你在哪里声明了单元格?根据你的评论进行了更新。Fogotten粘贴了单元格的声明部分。你在哪里声明了单元格?根据你的评论进行了更新。Fogotten粘贴了单元格的声明部分。你是对的。忘记了粘贴单元格的必要声明部分。另外,我看不到useOrigCellFromNib变量在y中的变化我们的代码?您从哪里获取arrS数组?请提供完整的原始代码您是对的。忘记粘贴单元格的必要声明部分。此外,我看不到useOrigCellFromNib变量在您的代码中如何更改?您从哪里获取arrS数组?请提供完整的原始代码