简体   繁体   English

UITableVIew中的自定义单元格外观

[英]Custom Cell look in UITableVIew

i was following this torturial: 我一直在关注这个折磨:

http://www.raywenderlich.com/32283/core-graphics-tutorial-lines-rectangles-and-gradients

It covers customization of dynamic table cells, i need to do it with static table cells. 它涵盖了动态表单元格的自定义,我需要使用静态表单元格来完成。

I have given every cell the identifier "Cell", as he does in the tutorial, i then subclassed the table view controller and implemented this: 正如他在本教程中所做的那样,我为每个单元格赋予了标识符“ Cell”,然后我将表视图控制器子类化并实现了这一点:

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


    // START NEW
    if (![cell.backgroundView isKindOfClass:[CostumCellBackground class]]) {
        cell.backgroundView = [[CostumCellBackground alloc] init];
    }


    if (![cell.selectedBackgroundView isKindOfClass:[CostumCellBackground class]]) {
        cell.selectedBackgroundView = [[CostumCellBackground alloc] init];
    }
    // END NEW


    cell.textLabel.backgroundColor = [UIColor clearColor]; // NEW

    return cell;
}

CostumCellBackground draws the rect. CostumCellBackground绘制矩形。

I am getting the error "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: 我收到错误消息:“ UITableView数据源必须从tableView:cellForRowAtIndexPath返回一个单元格:

As far as i understand UITableView is looped for every Cell in the storyboard, and it is supposed to return cell. 据我了解,UITableView在情节提要中为每个单元格循环,并且应该返回单元格。

So, whats going on here and why does the cell return nil, or doesnt return at all ? 那么,这是怎么回事,为什么单元格返回nil或根本不返回?

The only difference is that they are static tables, and not prototypes. 唯一的区别是它们是静态表,而不是原型。

If you are using Storyboards and iOS6 and your view controller is a UITableViewController, you will always get a cell if your cell identifier is present in your storyboard. 如果您使用情节提要和iOS6,并且视图控制器是UITableViewController,则如果情节提要中存在单元格标识符,则始终会得到一个单元格。 Checking for cel == nil is the old way to do it. 检查cel == nil是老方法。

Are you sure you have a custom cell in your storyboard with the "Cell" identifier? 您确定您的情节提要中有一个带有“单元格”标识符的自定义单元格吗?

Also, use this: 另外,使用此:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

If you look in the UITableView.h file you will find: 如果查看UITableView.h文件,则会发现:

// newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); 

Where is you create your cell ?? 您在哪里创建单元格?

You should add after: 您应该在以下位置添加:

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

such code (or something another initializer): 这样的代码(或其他初始化程序):

if (cell == nil) {
    cell = [[UITableViewCell alloc] init];   
}

Try this at the beginning of your method: 在方法开始时尝试以下操作:

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

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM