自定义uitableviewcell的问题

发布于 2024-12-01 17:30:03 字数 1755 浏览 0 评论 0原文

过去几天我一直在玩表格视图,试图适应它们,今天我一直在尝试创建一个自定义的 uitableviewcell,我正在苹果文档中的表格视图编程指南中工作,并且遇到了这个奇怪的问题我正在努力做。

我已经设置了一个自定义单元笔尖文件,它上面有两个标签,两个标签都有标签(0 和 1),我已将文件所有者类更改为将使用它的笔尖。

从那里我开始编写 tableView:cellForRowAtIndexPath: 方法,该方法在表视图中显示我的自定义单元格,但是正如您所见,当我为它分配一个字符串时,只显示我的第一个标签,我只是不知道为什么...

< strong>自定义单元笔尖 在此处输入图像描述

tableView:cellForRowAtIndexPath: method

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
            [[NSBundle mainBundle] loadNibNamed:@"ManufactureSearchCell" owner:self options:nil];
            cell = vehicleSearchCell;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            self.vehicleSearchCell = nil;
        }
        // Configure the cell...
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if(indexPath.section == 0)
        {
            if(indexPath.row == 0)
            {
                UILabel *label;
                label = (UILabel *)[cell viewWithTag:0];
                label.text = [[NSString alloc] initWithString:@"Manufacture :"];

                label = (UILabel *)[cell viewWithTag:1];
                label.text = [[NSString alloc] initWithString:@"empty"];
            }
            else if(indexPath.row == 1)
            {
//.....

如有任何帮助,我们将不胜感激:)

I have been playing around with tableviews for the past few days trying to get used to them, today I have been trying to create a custom uitableviewcell, I am working from the Table View Programming Guide in the apple documents and have this weird problem with something I am trying to do.

I have set up a custom cell nib file, it has two labels on it that both have tags (0 and 1) I have changed the File owners class to the nib that will be using it.

from there I have started programming the tableView:cellForRowAtIndexPath: method that displays my custom cells inside the tableview however as you will see, only my first label is showing when I assign it a string and I just don't know why...

Custom cell Nib
enter image description here

tableView:cellForRowAtIndexPath: method

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
            [[NSBundle mainBundle] loadNibNamed:@"ManufactureSearchCell" owner:self options:nil];
            cell = vehicleSearchCell;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            self.vehicleSearchCell = nil;
        }
        // Configure the cell...
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if(indexPath.section == 0)
        {
            if(indexPath.row == 0)
            {
                UILabel *label;
                label = (UILabel *)[cell viewWithTag:0];
                label.text = [[NSString alloc] initWithString:@"Manufacture :"];

                label = (UILabel *)[cell viewWithTag:1];
                label.text = [[NSString alloc] initWithString:@"empty"];
            }
            else if(indexPath.row == 1)
            {
//.....

Any help would be appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹏半生如梦愿梦如真 2024-12-08 17:30:03

请注意使用 viewWithTag 和 0。这是所有视图的默认标记,因此请仔细检查您是否确实从中获取了所需的标签,而不是单元格的背景视图、附件视图或添加到单元格的任何其他默认视图。它可能会导致问题。

如果您要重用该单元格,我建议创建一个自定义 UITableViewCell 子类并通过 IB 链接它们。如果您不重复使用该单元格,并且希望它只显示一次,那么一定要采纳 Karoly 的建议,将这些标签设置为 IBOutlet。

Beware using viewWithTag and 0. That is the default tag for all views, so double check you are actually getting the label you want back from that instead of the cell's background view, or accessory view, or any other default view added to the cell. It can cause problems.

If you are reusing the cell, I'd recommend creating a custom UITableViewCell subclass and link them through IB. If you are not reusing that cell, and want it to display only once, definitely take up Karoly's suggestion to just have those labels as IBOutlets.

请别遗忘我 2024-12-08 17:30:03

这让我觉得很奇怪——
为什么你使用

label.text = [[NSString alloc] initWithString:@"Manufacture :"];

而不是这样

label.text = @"Manufacture :";

,你不释放你的弦。使用 @"xxx" 简写创建一个自动释放的字符串。不确信这是问题的原因,但字符串的扭曲内存管理可能会产生像您所看到的效果。所以清理它将是一个好的开始。

另外,请确保在 IB 中使单元格具有对文件所有者的vehicleSearchCell 属性的引用。更多的是在黑暗中刺伤。

This strike me as odd-
Why do you use

label.text = [[NSString alloc] initWithString:@"Manufacture :"];

as opposed to

label.text = @"Manufacture :";

The way it is, your not releasing your strings. using the @"xxx" short hand creates a string that is autoreleased. Not confident that this is the cause of your problem but screwy memory mgmt with strings can produce effects like your seeing. So cleaning it up would be a good start.

Also, make sure that in IB you made the cell have a reference to the file owners' vehicleSearchCell property. More of a stab in the dark.

丘比特射中我 2024-12-08 17:30:03

我在代码中只看到一件事让我印象深刻。看起来您正在覆盖您的标签值。显示的是两者中的哪一个?

UILabel *label1;
label = (UILabel *)[cell viewWithTag:0];
label.text = [[NSString alloc] initWithString:@"Manufacture :"];

UILabel *label2
label2 = (UILabel *)[cell viewWithTag:1];
label2.text = [[NSString alloc] initWithString:@"empty"];

为了简单起见,我只声明两个标签。或者你已经这样做了?其他一切都正常吗?您是否以编程方式创建这些标签?如果不是,为什么不通过 IB 连接它们并给它们唯一的名称并以这种方式访问​​它们?你的 CellViewStyle 设置为什么?看起来您有一行将其设置为注释掉的内容,有什么具体原因吗?您还可以检查自定义单元格的 .xib 文件中的属性,看看是否有需要更改的内容。

I only see one thing in the code provided that sticks out to me. It looks like you are overwriting your label value. Which of the two is showing?

UILabel *label1;
label = (UILabel *)[cell viewWithTag:0];
label.text = [[NSString alloc] initWithString:@"Manufacture :"];

UILabel *label2
label2 = (UILabel *)[cell viewWithTag:1];
label2.text = [[NSString alloc] initWithString:@"empty"];

I would for simplicities sake just declare two labels. Or did you do that already? Is everything else working though? Are you programatically creating these labels? If not why don't you connect them through IB and give them unique names and access them that way? What is your CellViewStyle set to? It looks like you have the line that would set it to something commented out, is there any specific reason? You could also check your properties in the .xib file of your custom cell to see if there is something you need to change there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文