iPhone UITableViewCell CustomCell [英] Iphone UITableViewCell CustomCell

查看:94
本文介绍了iPhone UITableViewCell CustomCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试实现简单"的CustomCell, 我有一个普通的tableViewController,它可以使用普通的默认"方法很好地呈现, 但是我需要使用一些UILabel和一个UIImage实现一个Custom单元.

Attempting to implement a "Simple" a CustomCell, I have a normal tableViewController that renders fine using the normal "default" methods, but I need to implement a Custom cell with some UILabel's and a UIImage.

所以我创建了CustomCell.h,CustomCell.m,CustomCell.xib

So I created the CustomCell.h, CustomCell.m, CustomCell.xib

.H

@interface CustomCell : UITableViewCell <UITextViewDelegate>
{
    IBOutlet UIImageView *image;
    IBOutlet UILabel *name;
    IBOutlet UILabel *date;
    IBOutlet UILabel *comment;
}

@property (retain,nonatomic) IBOutlet UIImageView *image;
@property (retain,nonatomic) IBOutlet UILabel *name;
@property (retain,nonatomic) IBOutlet UILabel *date;
@property (retain,nonatomic) IBOutlet UILabel *comment;

和.M

@implementation CustomCell

@synthesize image;
@synthesize name;
@synthesize date;
@synthesize comment;


#pragma mark -
#pragma mark View lifecycle

- (id) initWithController: (Controller *) ctnlr 
{
    ControllerPointer = ctnlr;
    return(self);
}

- (void) SetImage:(UIImageView*)Image
{
    image = Image;
}

- (void) SetName:(NSString*)Name
{
    [Name retain];
    [name.text release];
    name.text = Name;
}

- (void) SetDate:(NSString*)Date
{
    [Date retain];
    [date.text release];
    date.text = Date;
}

- (void) SetComment:(NSString*)Comment
{
    [Comment retain];
    [comment.text release];
    comment.text = Comment;
}

无论如何,当我尝试在cellForRowAtIndexPath中创建这些定制单元时(可能会实现这一点),我只剩下一个空白屏幕.所以很明显我缺少一些大东西……当我使用接口生成器"创建.XIB文件时,我确保将参考插座"连接到适当的标签和图像.

anyway, when I attempt to create these customcells in cellForRowAtIndexPath (as one would expect might be implemented) I am left with only a blank screen. So obviously I am missing something big... When I created the .XIB file with "Interface Builder" I made sure to connect the "Referencing Outlets" to the appropriate labels and images.

因此,遵循Xcode框架似乎起作用的方式的隐含逻辑, 我遵循相同的推理(由于缺少确切的示例),没有用... 无论如何,如果有任何iPhone极客想给我启发... (是的,没有"[something release]"调用,我什至不确定是否需要分配任何东西.请告诉我,我只剩下几个调用,不能超过像这样的简单东西吧??

So following the implied logic of the way the Xcode framework appears to work, I followed the same reasoning (for lack of an exact example) No worky... Anyway, if there are any IPhone geeks that would like to enlighten me... (yes, there are no "[something release]" calls, I am not even sure if anything needed to be alloc'd. Please tell me there's just a couple calls I am leaving out, it can't be too much more than something simple like this Right...?

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

    if(cell == nil)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] lastObject];
    }

    NSUInteger row = [indexPath row];
    SnsObject *sObj = [SnsArray objectAtIndex:row]; 
    [cell SetName:[sObj getUserName]];

    NSUInteger row = [indexPath row];
    SnsObject *sObj = [SnsArray objectAtIndex:row]; 
    cell.name = [[UILabel alloc]init];
    cell.name.text = [sObj getUserName];

    cell.date = [[UILabel alloc]init];
    cell.date.text = [sObj getDateTime];

    cell.comment = [[UILabel alloc]init];
    cell.comment.text = [sObj getCommentText];

    cell.image = [[UIImageView alloc]init];
    cell.image.image = [sObj getImageUrl];
  return(cell)
}

预先感谢!

推荐答案

除了mrcrowl提到的有关现在需要分配-初始化"出口的代码外,代码还有其他问题.特别是这一行:

There are other issues with the code beyond what mrcrowl mentioned about now needing to "alloc-init" the outlets. In particular, this line:

cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] lastObject];

这不是用于从.xib加载自定义表格视图单元格的典型习语.首先,您传递"owner:self",这意味着您想将.xib文件中的出口对象与 tableviewcontroller 对象中的出口成员连接起来,可能不是您想要的. 其次,您依赖于从loadNibNamed:owner:options:返回的对象的顺序,虽然该顺序可能在今天起作用,但明天可能不起作用,或者依赖于新版本的iOS.

This is not the typical idiom used to load a custom tableview cell from a .xib. First of all, you pass "owner:self", which means you want to hook up the outlet objects in the .xib file with outlet members in your tableviewcontroller object, probably not what you intended. Second, you're relying on the order of objects returned from loadNibNamed:owner:options:, which while it may work today, may not work tomorrow, or on a new release of iOS.

相反,通常的习惯用法是在tableviewcontroller中为整个tableviewcell声明一个出口: (在.h文件中):

Instead, the usual idiom is to declare an outlet for the entire tableviewcell in your tableviewcontroller: (in the .h file):

...
   UITableViewCell *tvCell;
...

@property (nonatomic, retain) IBOutlet UITableViewCell *tvCell;

然后代替您的行,您需要:

Then in place of your line, you have this:

[[NSBundle mainBundle] loadNibNamed:@"NewsArchiveTitleTvCell" owner:self options:nil];
cell = tvCell;
self.tvCell = nil;

通常这不是通过子类完成的,请注意我没有将类声明为CustomCell,而是将其声明为普通的UITableViewCell.那么,如何获得这些讨厌的子视图,以便对其进行配置?使用标签是正常的方式:

Normally this isn't done with subclassing, notice how I didn't declare the class as CustomCell, but as a vanilla UITableViewCell. So how to you get at those pesky subviews so you can configure them? Using tags is the normal way:

...
#define kMyKewlLabelTag 1
...
    UILabel *kewlLabel = (UILabel *) [cell viewWithTag:kMyKewlLabelTag];
    kewlLabel.text = [NSString stringWithFormat:@"Hi there from row %d!", indexPath.row];
...

这里有更多细节,评论太短而无法解决这里发生了什么?"问题.这是我的一个应用程序的摘录,该应用程序是从.xib加载UITableViewCell的:

edit: here's a bit more detail, comments are too short to address the "what's going on here?" question. Here's an excerpt from one of my apps that loads the UITableViewCell from a .xib:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"MyShoppingCartTvCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        [[NSBundle mainBundle] loadNibNamed:@"ShoppingCartTvCell" owner:self options:nil];
        cell = tvCell;
        self.tvCell = nil;
    }
        ...
        // (insert code to grab model data for this row)
        ...
    UILabel *nameLabel = (UILabel *) [cell viewWithTag:1];
    nameLabel.text = itemNameStr;

    UILabel *countLabel = (UILabel *) [cell viewWithTag:2];
    countLabel.text = [NSString stringWithFormat:@"%d", itemCount];

    UIImageView *iv = (UIImageView *) [cell viewWithTag:3];
        ...

这是这里发生的事情:

  • 没有自定义UITableViewCell子类,只有一个名为"ShoppingCartTvCell.xib"的.xib文件,其中包含一个UITableViewCell和位于UITableViewCell内的UI元素.为每行数据必须更改的UI元素分配了一个唯一标签(标签字段位于IB的CMD-1属性检查器中),以便您的代码可以获取要更改这些对象的句柄(自定义标签,图像等) .确保您不使用"0",因为默认情况下所有元素都带有0标签.另外,还要确保CMD-1属性检查器中的UITableViewCell的Identifier字段是CellIdentifier字符串.

  • There is no custom UITableViewCell subclass, there is only a .xib file named "ShoppingCartTvCell.xib" containing a UITableViewCell, and UI elements placed inside the UITableViewCell. UI elements whose data must change per row are assigned a unique tag (the tag field is in the CMD-1 Attributes Inspector in IB) so that your code can get a handle to those objects to change them (customize labels, images, etc). Make sure you don't use "0" since all elements by default have a 0 tag. Also, make sure the Identifier field of the UITableViewCell in CMD-1 Attributes Inspector is the CellIdentifier string.

.xib文件的文件所有者是您要在其中显示单元格的表视图控制器.更准确地说,它可以是任何包含IBOutlet UITableViewCell * tvCell;的类.成员.它是您作为所有者传递给loadNibNamed:owner:options:的此类的一个实例.只要所有者中链接的出口的值为nil,当您调用loadNibNamed:owner:options时,所有者的出口就会被来自.xib的对象填充(只要在.xib中进行了连接). IB中的xib).了解这一点是Apple编程中的一个神奇时刻,它将为您打开全新的面纱:).

The File's Owner of the .xib file is your table view controller where you want to display the cell. More precisely, it can be any class containing a IBOutlet UITableViewCell *tvCell; member. It is an instance of this class that you pass in as owner to loadNibNamed:owner:options:. As long as the value of the linked outlet is nil in the owner, when you call loadNibNamed:owner:options, the outlet of the owner is filled in with the object from the .xib (as long as the connection was made in the .xib in IB). Understanding that is a magic moment in Apple programming that opens whole new vistas to you :).

您必须设置self.tvCell = nil;以准备需要从.xib加载的下一个cellForRowAtIndexPath.有时我还可以在loadNibNamed:owner:options:之前设置为nil,实际上我认为这会更安全.

You must set self.tvCell = nil; to prepare for the next cellForRowAtIndexPath that needs to load from the .xib. I also sometimes set to nil before loadNibNamed:owner:options:, I think that's a bit safer actually.

这是从.xib加载UITableViewCells的方法:

Here's how you go about loading your UITableViewCells from a .xib:

  • 在xcode中,添加IBOutlet UITableViewCell * tvCell;到您的UITableViewController类(如果需要,包括属性声明)

  • In xcode, add an IBOutlet UITableViewCell *tvCell; to your UITableViewController class (including property declaration if you like)

在您的xcode项目中,创建一个新文件,用户界面,Empty Xib.在IB中打开此.xib

In your xcode project, create a New File, User Interface, Empty Xib. Open this .xib in IB

在IB中,将TableViewCell从库中拖到第一响应者"下的空.xib文件中.

In IB, drag a TableViewCell from the Library into your empty .xib file under First Responder

单击文件的所有者CMD-4(标识检查器),然后在类"下选择包含您添加的IBOutlet UITableViewCell * tvCell的类(可能是UITableViewController子类,用于操纵表的类).

Click File's Owner, CMD-4 (Identify Inspector), and under Class select the class containing the IBOutlet UITableViewCell *tvCell that you added (probably your UITableViewController subclass, the class where you're manipulating your table).

按住Control键从File的所有者拖到UITableViewCell,然后选择要连接的插座.当您使用File Owner的实例作为"owner"参数调用loadNibNamed:owner:options时,此字段将保存从Xib新加载的UITableViewCell.

Control-drag from File's owner to the UITableViewCell, and select the outlet you want to hook up. This is the field that will hold the newly-loaded-from-xib UITableViewCell when you call loadNibNamed:owner:options with an instance of File's Owner as the "owner" parameter.

将UI元素添加到UITableViewCell中(确保它们位于UITableViewCell层次结构内部 中).您要按行自定义的任何元素都需要一个唯一的标记值.

Add UI elements into the UITableViewCell (make sure they're inside the UITableViewCell hierarchy). Any elements that you want to customize per-row require a unique tag value.

按照我上面在cellForRowAtIndexPath中给出的食谱

follow the recipe I gave above in cellForRowAtIndexPath

有一个神奇的时刻,您可以开始了解.xib文件和File的Owner对象是如何真正协同工作的,并开始在IB中创建许多很棒的UITableViewCells和其他自定义视图对象,因为它确实比创建更容易并且更好他们用代码(IMNSHO).

Have a magic moment where you start to understand how .xib files and File's Owner objects really work together, and start creating lots of cool UITableViewCells and other custom view objects in IB because it's really easy and way better than creating them in code (IMNSHO).

这篇关于iPhone UITableViewCell CustomCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆