tableView问题中的自定义单元格

时间:2012-08-29 12:43:28

标签: iphone tableview cell

我正在尝试通过以下代码在TableView中创建自定义单元格。我是Objective C和Iphone开发的100%新手,所以我远离经验丰富。

在下面的代码中: cell.label1 = [[UILabel alloc] initWithFrame:labelFrame]; 它表示(在'UITableViewCell'类型的对象上找不到属性'label1':

我错过了什么?所有相关代码都可以在下面找到。所有帮助/初学者的Thanx

BookmarksViewController.h

@interface BookmarksViewController : UITableViewController <UIAlertViewDelegate>
{
NSMutableArray *items;

UILabel *label1;
UILabel *label2;
UILabel *label3;


}
@property (nonatomic, retain) NSMutableArray *items;
@property (nonatomic, retain) UILabel *label1;
@property (nonatomic, retain) UILabel *label2;
@property (nonatomic, retain) UILabel *label3;

- (NSMutableArray*) getStoredItems;
- (void) clearItems;
- (void) removeItemWithId:(int)itemId;

@end

BookmarksViewController.m

#import "BookmarksViewController.h"

@interface BookmarksViewController ()

@end

@implementation BookmarksViewController

@synthesize items;
@synthesize label1 = _label1;
@synthesize label2 = _label2;
@synthesize label3 = _label3;



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

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

    CGRect labelFrame = cell.bounds;
    labelFrame.size.height= labelFrame.size.height * 0.5;

    cell.label1 = [[UILabel alloc] initWithFrame:labelFrame];

}
Bookmark *item = [self.items objectAtIndex:indexPath.row];

NSArray *chunks = [item.name componentsSeparatedByString: @","];

NSString *name;
NSString *book;
NSString *chapter;

if ([chunks count] > 0)
{
    name = [chunks objectAtIndex:0];
    if ([chunks count] > 1)
    {
        book = [chunks objectAtIndex:1];
        if ([chunks count] > 2)
        {
            chapter = [chunks objectAtIndex:2];
        }
    }
}

cell.label1.text = name;

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70; // height of tableView Cell
}

4 个答案:

答案 0 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d", indexPath.row] ;
    UILabel *lblTitle = nil;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        lblTitle =[[UILabel alloc]initWithFrame:CGRectMake(5, 0, 320, 44)];
        lblTitle.backgroundColor = [UIColor clearColor];
        lblTitle.font = [UIFont systemFontOfSize:14.0];
        lblTitle.tag = 1;

        UILabel *lblType =[[UILabel alloc]initWithFrame:CGRectMake(5, 20, 320, 44)];
        lblType.backgroundColor = [UIColor clearColor];
        lblType.font = [UIFont systemFontOfSize:14.0];
        lblType.tag = 2;

        UILabel *lblDate =[[UILabel alloc]initWithFrame:CGRectMake(5, 40, 320, 44)];
        lblDate.backgroundColor = [UIColor clearColor];
        lblDate.font = [UIFont systemFontOfSize:14.0];
        lblDate.tag = 3;

        UILabel *lblTime =[[UILabel alloc]initWithFrame:CGRectMake(155, 20, 320, 44)];
        lblTime.backgroundColor = [UIColor clearColor];
        lblTime.font = [UIFont systemFontOfSize:14.0];
        lblTime.tag = 4;

        [cell.contentView addSubview:lblTitle];
        [cell.contentView addSubview:lblType];
        [cell.contentView addSubview:lblDate];
        [cell.contentView addSubview:lblTime];

    }

    lblTitle = (UILabel*)[cell viewWithTag:1];


    [lblTitle setText:@""];



    UILabel *lblType = (UILabel *) [cell.contentView viewWithTag:2];
    [lblType setText:@""];

    UILabel *lblDate = (UILabel *) [cell.contentView viewWithTag:3];
    [lblDate setText:@""];
    return cell;
}

答案 1 :(得分:0)

您需要将标签添加到单元格,因为单元格本身没有label1属性等 -

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

    NSUInteger row = [indexPath row];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    } else { // if there is already a subview, remove it
    while ([[cell.contentView subviews] count] > 0) {
        UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
    [labelToClear removeFromSuperview];
    }
    }

    UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 5, 400, 30)] autorelease];

    // set up myLabel here

    [cell.contentView addSubview:myLabel];


    return cell;

}

答案 2 :(得分:0)

编写此代码:

label1 = [[UILabel alloc] initWithFrame:labelFrame];
    [cell.contentView addSubview:label1];

而不是cell.label1 = [[UILabel alloc] initWithFrame:labelFrame];

以及label1.text = name;代替cell.label1.text = name;

我认为这会对你有所帮助。

答案 3 :(得分:0)

你必须做的事情很少: 1.创建子类UITableViewCell的自定义类(.h和.m)。 2.然后在您的故事板中,将tableview单元格的类名称作为创建的类。 3.现在,您可以在创建的自定义类中设计单元格并连接插座。 4.另外不要忘记在故事板中给出单元格的重用标识符。 5.现在在CellForRowAtIndexPath方法中,您需要编写以下代码:

-

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


    YourCustomClass *cell = [tableView dequeueReusableCellWithIdentifier:@"YourReuseIdentifier"];
    if (cell == nil) {
        cell = [[YourCustomClass alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:@"YourReuseIdentifier"];
    }


cell.yourLabel.text = cell.cityNameLbl.text.capitalizedString;
return cell;

}