佳为好友

原:添加自定义的UITableViewCell -4

创建:100721
修改:101221
修改:110418

添加自定义的UITableViewCell有2种方法,其一是使用contextView;其二是使用UITableViewCell的派生类。
对于第一种,则只需要产生0-1个新文件。所以,我只介绍第一种。
对于第二种,则一定要产生2-3个新文件。所以,我不喜欢使用。但是,可以参考CookBook第二版的“自定义Cell”的介绍。


1,首先创建自定义的contentView。使用xib文件创建。创建分两种情况:其一,如果你的UITableView有自己的xib文件,则可以在改xib文件中,加入新的一个view,作为contentView。其二,如果UITableView没自己的xib文件,则需要创建一个xib文件,用于放置新的contentView。

新的contentView的宽度是cell的宽度,根据我的经验,应该是284。高度则根据情况自定义。
对于需要修改内容的控件,如文本框等,则需要指定一个tag。

2,在自定义的UITableViewController的派生类中,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"OptionViewModuleListControllerCell";
        
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

//        UIView *subView = [[[NSBundle mainBundle] loadNibNamed:@"OptionViewModuleListControllerCellViewXibFile" 
//                                                         owner:self options:NULL] lastObject]; 
        UIView *subView = nil;
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[UIView class]])
                subView = (UIView *)oneObject;

        [(UIButton *)[subView viewWithTag:104] addTarget: self action:@selector(onBuyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:subView];
        CGRect subViewFrame = subView.frame;
        subViewFrame.origin = CGPointMake(10, 10); //  对于group类型的cell,则圆角范围,大约是10像素。
        subView.frame = subViewFrame;        
    }
    
    // Set up the cell...
    UIView *subView = [cell.contentView.subviews objectAtIndex:0];

    
    UILabel *titleLabel = (UILabel *)[subView viewWithTag:101]; 
    UILabel *descriptionLabel = (UILabel *)[subView viewWithTag:103]; 
    UILabel *buyStatusLabel = (UILabel *)[subView viewWithTag:102];

//.......................
//.......................
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140.0; // 这个高度大约是contentView的高度+10*2像素。 这10像素就是cell的圆角空间。
}

2010-12-21补充:
如果你在cell上加入了一个button。则在button的响应函数中,如果想得到对应的cell和indexPath,则可以:
- (IBAction)buttonWasPressed:(id)sender
{
    NSIndexPath *indexPath =
        [self.myTableView
         indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
    NSUInteger row = indexPath.row;

    // Do something with row index
}
注意,如果你的button不是直接加入到contentView中,则superview的关系可能不是仅仅2层,这个就需要具体问题,具体分析了。

===============
2011.04.18补充:
上述方法虽然使用文件较少,但是有个弊端。如果该Cell的控件较多,那么导致使用tag,以及获取viewWithTag的太多,很麻烦,也很乱。此时的一个做法是:
创建类,例如CustomView。
然后,在xib中,将该View的类型改为CustomView。然后进行IBOutlet的关联。
然后,在使用上述方法,得到该CustomView。同时,也得到了相应的IBOutlet。
这种方法,跟CookBook的方法差不多。但是有个本质的区别,就是,CookBook中,CustomView的类型是UITableViewCell。这样,在执行LoadNib之后,直接就可以使用该Cell了,不必在[cell.contentView addSubview:aCustom]了。但是,这种方法的缺点很大,很大。最重要的缺点是:无法使用Cell的Reuse功能了。因为整个Cell都是通过LoadNib得到的,而非[[UITableViewCell alloc] initWithStyle:
x reuseIdentifier:x];,因此无法使用reuse机制。
因此,我们不使用Cookbook的方法。

+++++

posted on 2012-12-30 11:39 佳为好友 阅读(565) 评论(0)  编辑 收藏 引用 所属分类: UI


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2012年12月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

留言簿(1)

随笔分类

搜索

最新评论

评论排行榜