Iphone UITableViewCell自定义单元格

Iphone UITableViewCell自定义单元格,iphone,uitableview,Iphone,Uitableview,试图在CustomCell中实现“简单”的, 我有一个普通的tableViewController,可以使用普通的“默认”方法进行精细渲染, 但是我需要用一些UILabel和UIImage实现一个自定义单元格 所以我创建了CustomCell.h,CustomCell.m,CustomCell.xib H @interface CustomCell : UITableViewCell <UITextViewDelegate> { IBOutlet UIImageView *i

试图在CustomCell中实现“简单”的, 我有一个普通的tableViewController,可以使用普通的“默认”方法进行精细渲染, 但是我需要用一些UILabel和UIImage实现一个自定义单元格

所以我创建了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;
无论如何,当我试图在cellForRowAtIndexPath中创建这些customcells时(正如人们所期望的那样),我只剩下一个空白屏幕。所以很明显我错过了一件大事。。。当我使用“Interface Builder”创建.XIB文件时,我确保将“参考插座”连接到适当的标签和图像

因此,遵循Xcode框架工作方式的隐含逻辑, 我遵循同样的推理(因为缺少一个确切的例子)不工作。。。 无论如何,如果有任何IPhone极客想启发我。。。 (是的,没有“[发布内容]”的电话,我甚至不确定是否需要分配任何内容。请告诉我,我只遗漏了几个电话,不会比这样简单的事情更多吧…?)

- (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)
}

提前感谢!

从.xib加载UITableViewCell时,不需要手动创建控件

例如,这种事情是不必要的:

cell.name = [[UILabel alloc]init];
这将用一个具有零帧的新标签替换从xib加载的标签——即,新标签将位于0,0,并且没有宽度或高度。因此,没有工作

假设您已将xib正确连接到CustomCell的IBOutlets,您正在寻找的控件应该已经存在

注意:如果我对您的方法名称读得太多,请原谅,但我认为这一行也行不通,因为.image属性需要UIImage:

cell.image.image = [sObj getImageUrl];

从.xib加载UITableViewCell时,不需要手动创建控件

例如,这种事情是不必要的:

cell.name = [[UILabel alloc]init];
这将用一个具有零帧的新标签替换从xib加载的标签——即,新标签将位于0,0,并且没有宽度或高度。因此,没有工作

假设您已将xib正确连接到CustomCell的IBOutlets,您正在寻找的控件应该已经存在

注意:如果我对您的方法名称读得太多,请原谅,但我认为这一行也行不通,因为.image属性需要UIImage:

cell.image.image = [sObj getImageUrl];

除了mrcrowl提到的现在需要“alloc init”出口之外,代码还有其他问题。特别是这一行:

cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] lastObject];
这不是从.xib加载自定义tableview单元格时使用的典型习惯用法。首先,传递“owner:self”,这意味着您希望将.xib文件中的outlet对象与tableviewcontroller对象中的outlet成员连接起来,这可能不是您想要的。 其次,您依赖于从loadNibNamed:owner:options:返回的对象的顺序,虽然它今天可以工作,但明天可能无法工作,或者在新版本的iOS上

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

然后,您可以使用以下内容代替您的线路:

[[NSBundle mainBundle] loadNibNamed:@"NewsArchiveTitleTvCell" owner:self options:nil];
cell = tvCell;
self.tvCell = nil;
通常情况下,子类化不会完成这项工作,请注意,我没有将该类声明为CustomCell,而是将其声明为vanilla UITableViewCell。那么,如何获取这些讨厌的子视图以便进行配置?使用标记是正常的方法:

...
#define kMyKewlLabelTag 1
...
    UILabel *kewlLabel = (UILabel *) [cell viewWithTag:kMyKewlLabelTag];
    kewlLabel.text = [NSString stringWithFormat:@"Hi there from row %d!", indexPath.row];
...
编辑: 编辑:这里有一点更详细,注释太短,无法回答“这里发生了什么?”问题。下面是我从.xib加载UITableViewCell的一个应用程序的摘录:

- (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子类,只有一个名为“ShoppingCartVcell.xib”的.xib文件包含UITableViewCell,并且UI元素放置在UITableViewCell内。每行数据必须更改的UI元素被分配一个唯一的标记(标记字段位于IB中的CMD-1属性检查器中)因此,您的代码可以获得这些对象的句柄来更改它们(自定义标签、图像等)。请确保不要使用“0”,因为默认情况下,所有元素都有一个0标记。此外,请确保CMD-1属性检查器中UITableViewCell的标识符字段是CellIdentifier字符串

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

  • 必须设置
    self.tvCell=nil
    为需要从.xib加载的下一个cellForRowAtIndexPath做准备。有时,我在加载nibNamed:owner:options:之前也会将其设置为nil,我认为这样实际上更安全一些

以下是如何从.xib加载UITableViewCells:

- (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];
        ...
  • 在xcode中,添加一个IBUITableViewCell*tvCell;到UITableViewController类(如果愿意,包括属性声明)

  • 在xcode项目中,创建一个新文件、用户界面、空Xib。在IB中打开这个.xib

  • 在IB中,将TableViewCell从库拖动到First Responder下的空.xib文件中

  • 单击文件的所有者CMD-4(Identify Inspector),然后在“类”下选择包含您添加的IBOutlet UITableViewCell*tvCell的类(可能是您的UITableViewController子类,您正在操作表的类)

  • 控件从文件的所有者拖动到