0

I am trying to create a custom cell through the following code below in my TableView. I am 100% new to Objective C and Iphone development so I am far away from experienced.

On the following code: cell.label1 = [[UILabel alloc] initWithFrame:labelFrame]; it says (Property 'label1' not found on object of type 'UITableViewCell':

What have I missed? All the relevant code can be found below. Thanx for all the help / beginner

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

4 回答 4

0

为此,您需要做的几件事: 1. 创建 UITableViewCell 子类的自定义类(.h 和 .m)。2. 然后在您的故事板中将表格视图单元格的类名作为创建的类。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;

}

于 2014-01-06T13:21:00.980 回答
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;

}
于 2012-08-29T12:57:11.183 回答
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;
}
于 2012-08-29T12:56:05.400 回答
0

写下这段代码:

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

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

而且也label1.text = name;代替cell.label1.text = name;

我想这会对你有所帮助。

于 2012-08-29T13:00:37.143 回答