Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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
Objective c 自定义UITableViewCell问题_Objective C_Cocoa Touch - Fatal编程技术网

Objective c 自定义UITableViewCell问题

Objective c 自定义UITableViewCell问题,objective-c,cocoa-touch,Objective C,Cocoa Touch,我使用以下说明创建了一个自定义单元格类: 现在,我有两个视图控制器:MyPostsViewController和CustomCellViewController。自定义单元格只是一个带有UITableViewCell的单元格,并且声明并连接了两个标签。MyPostsViewController是使用这些自定义单元格的UITableView。但是,我不确定如何从MyPostsViewController访问CustomCellViewController的两个标签。我想从MyPostsViewCo

我使用以下说明创建了一个自定义单元格类:


现在,我有两个视图控制器:
MyPostsViewController
CustomCellViewController
。自定义单元格只是一个带有
UITableViewCell
的单元格,并且声明并连接了两个标签。
MyPostsViewController
是使用这些自定义单元格的
UITableView
。但是,我不确定如何从
MyPostsViewController
访问
CustomCellViewController
的两个标签。我想从
MyPostsViewController
访问它们,因为
cellforrowatinexpath
方法就在这里,我想在这里设置标签的值。我该怎么做呢?

如果您按照发布的说明操作,就不需要
CustomCellViewController
。只需将NIB中的单元格加载到
MyPostsViewController


要访问单元格的各个子视图,请为每个子视图指定一个独特的
标记
,然后使用
[单元格视图WithTag:42]
检索它们。例如,如果按照发布的说明操作,则不需要
CustomCellViewController
。只需将NIB中的单元格加载到
MyPostsViewController

要访问单元格的各个子视图,请为每个子视图指定一个独特的
标记
,然后使用
[单元格视图WithTag:42]
检索它们。

您应该在
单元格中自定义(设置标签的值)
。为此,可以在IB中为标签设置标签(在属性检查器中找到标签字段)。假设您为Label1设置了1,为Label2设置了2。然后,您的代码看起来像(从您发布的链接复制)-

嗯,

Akshay

您应该在
cellforrowatinexpath:
中自定义(设置标签的值)。为此,可以在IB中为标签设置标签(在属性检查器中找到标签字段)。假设您为Label1设置了1,为Label2设置了2。然后,您的代码看起来像(从您发布的链接复制)-

嗯,


Akshay

我认为最干净的方法是在自定义UITableViewCell子类上定义IBOutlets。然后,在设计XIB时,按住CTRL键并单击自定义单元格。你应该看看那里的插座。拖放以将您的插座连接到标签,就像您通常使用视图一样。最后,在cellForRowAtIndexPath方法中访问这些IBOutlets。

我认为最干净的方法是在自定义UITableViewCell子类中定义IBOutlets。然后,在设计XIB时,按住CTRL键并单击自定义单元格。你应该看看那里的插座。拖放以将您的插座连接到标签,就像您通常使用视图一样。最后,在cellForRowAtIndexPath方法中访问这些IBoutlet。

很抱歉,我真的不明白如果不是这样,我会怎么做?我只是按照第二种方法的说明进行操作。通过使用第二种方法(
NSBundle:loadNibNamed:owner:options:
),您不需要这样的
CustomCellViewController
…看看这个:那么我的自定义单元格应该设置在哪里?在MyPostsViewController.xib文件中?我在这些链接中看不到解决方案。这个呢?我很抱歉,但我真的不明白如果不是这样,我会怎么做?我只是按照第二种方法的说明进行操作。通过使用第二种方法(
NSBundle:loadNibNamed:owner:options:
),您不需要这样的
CustomCellViewController
…看看这个:那么我的自定义单元格应该设置在哪里?在MyPostsViewController.xib文件中?我在这些链接中看不到解决方案。这个呢?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }

//customize the cell here
UILabel* label 1 = [cell viewWithTag:1];
label1.text = @"my text";

//similarly label 2

    return cell;
}