Iphone tableView中的自定义单元格问题
我正试图通过下面的代码在我的TableView中创建一个自定义单元格。我对Objective C和Iphone开发是100%的新手,所以我远没有经验 在以下代码中:cell.label1=[[UILabel alloc]initWithFrame:labelFrame]它表示(在“UITableViewCell”类型的对象上找不到属性“label1”: 我遗漏了什么?所有相关代码都可以在下面找到。谢谢所有帮助/初学者 BookmarksViewController.hIphone tableView中的自定义单元格问题,iphone,tableview,cell,Iphone,Tableview,Cell,我正试图通过下面的代码在我的TableView中创建一个自定义单元格。我对Objective C和Iphone开发是100%的新手,所以我远没有经验 在以下代码中:cell.label1=[[UILabel alloc]initWithFrame:labelFrame]它表示(在“UITableViewCell”类型的对象上找不到属性“label1”: 我遗漏了什么?所有相关代码都可以在下面找到。谢谢所有帮助/初学者 BookmarksViewController.h @interface Bo
@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
您需要将标签添加到单元格中,因为单元格本身没有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;
}
编写以下代码:
label1 = [[UILabel alloc] initWithFrame:labelFrame];
[cell.contentView addSubview:label1];
而不是cell.label1=[[UILabel alloc]initWithFrame:labelFrame];
还有label1.text=name;
而不是cell.label1.text=name;
我认为这会对您有所帮助。为此您需要做几件事: 1.创建子类UITableViewCell的自定义类(.h和.m)。 2.然后在故事板中,将tableview单元格的类名作为创建的类。 3.现在您可以在创建的自定义类中设计单元并连接插座。 4.也不要忘记在故事板中给出单元的重用标识符。 5.现在在CellForRowatineXpath方法中,您需要编写以下代码: -
}您将label1创建为BookmarksViewController类的属性,而不是UITableViewCell类。此外,如果要创建自定义单元格,通常应创建自定义单元格类作为UITableViewCell的子类
-(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;
}
label1 = [[UILabel alloc] initWithFrame:labelFrame];
[cell.contentView addSubview:label1];
(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;