Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.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
Iphone 自定义UITableView子类单元格?_Iphone_Objective C_Uitableview - Fatal编程技术网

Iphone 自定义UITableView子类单元格?

Iphone 自定义UITableView子类单元格?,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我在Interface Builder中创建了自定义单元格,并为其创建了自定义UITableViewCell类,但在加载该单元格时,不会对其进行任何更改。我在自定义类中有以下代码: - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdenti

我在Interface Builder中创建了自定义单元格,并为其创建了自定义UITableViewCell类,但在加载该单元格时,不会对其进行任何更改。我在自定义类中有以下代码:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
        // Initialization code

        //Get the wedding date and display it
        myLabel.text = @"Hello";
    }
    return self;
}
myLabel已在标题中声明,具有属性,并已在Interface builder中链接,但当我运行应用程序并查看我的表时,我没有收到“Hello”文本。有什么想法吗

谢谢

编辑:

我没有使用nib文件,而是将其放在故事板中的UITableViewController中。另外,下面是CellForRowatineXpath代码,我只需要用所需的单元格标识符填充一个数组,然后创建所述数组:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier;

    for (int cell = 0; cell <= [tableCellsArray count]; cell++) 
    {
        if ([indexPath row] == cell) 
        {
            CellIdentifier = [tableCellsArray objectAtIndex:cell];
        }
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{    
静态NSString*CellIdentifier;
对于(int cell=0;cell),请浏览,其中介绍了如何自定义UITableViewCell


在上面的页面中,请参见清单5-5
从nib文件加载单元格并为其分配内容
,它清楚地描述了如何使用nib文件自定义单元格。

如何创建单元格?是从nib加载还是分配? 如果分配它,则需要正确初始化实例字段(例如myLabel)。 如果你用笔尖装电池,我担心

  • (id)initWithCoder:(NSCoder*)aDecoder
是要重写的init方法

无论如何,您可以设置一个断点以确保通过该断点,然后打印myLabel以确保其正确初始化

--编辑

如果希望使用nib拥有自定义的单元视图,则不能使用initWithStyle:reuseIdentifier:method初始化单元视图

使用initWithStyle:reuseIdentifier:意味着您将通过自己的实现初始化所有视图(例如,您将分配、初始化和配置所有视图,它不会使用xib文件)

如果要使用xib文件,需要在NSBundle中使用loadNibNamed:owner:options:method

通常,我看到了两种实现:

[[NSBundle mainBundle]loadNibNamed:@“所有者:自选项:nil]

在这个实现中,加载xib时将UIViewController子类作为所有者。在xib中,确保文件所有者类是UIViewController子类。然后只需将单元格与UIViewController子类的属性链接,并调用loadNibNamed:owner:options:将只分配和初始化一个新的单元格视图,该视图将e在UIViewController子类的属性中可用。这是Apple文档建议您执行的操作

NSArray*nibObjects=[[NSBundle mainBundle]loadNibNamed:@“所有者:无选项:无]; UITableViewCell myCustomCell=[nibObjects对象索引:0]

在这个实现中,您不会向xib文件传递任何所有者并获取所需的对象。只需确保您的xib文件只有一个对象,即单元格视图。我更喜欢这种方法,因为我认为它“很奇怪”要创建单元格,请使用属性,但需要确保xib文件的内容将自定义单元格视图保留在0索引中

无论您选择什么方法,请记住加载xib都会调用

initWithCoder: 而不是 initWithStyle:reuseIdentifier:


最后,我建议您去看看Aadhira指向Apple文档的链接,该文档举例说明了如何将自定义UITableCellView与xib文件一起使用。

您是否也可以添加cellForRowAtIndexPath方法的代码?这是静态还是动态单元格?另外,myLabel的声明属性是
还是
?(它很可能是强的。)好的,它是动态的。是否加载了多个单元格?如果是,该属性将只包含对上一个创建的单元格的引用。顺便说一下,您可以用以下内容替换整个for{}循环:
CellIdentifier=[tableCellsArray objectAtIndex:[indexPath row]];
它做同样的事情,而且更加清晰。