我是一个新手,请您多多包涵:
在IB中,我有一个包含UITableView的UIScrollView的.xib。 我想将自定义的UITableViewCells加载到UITableView中。 我已经在IB中创建了四五个自定义单元格,但无法正确加载第一个。
以下是IB中相关的屏幕截图:
我已将在IB中创建的自定义UITableViewCell的类设置为我创建的UITableView类的“itemCell”。此外,我还将三个文本字段的输出和代理连接到“itemCell”。
这是itemCell.h:
#import <UIKit/UIKit.h>
@interface itemCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UITextField *quantityTextField;
@property (weak, nonatomic) IBOutlet UITextField *itemTextField;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;
@end
这里是itemCell.m文件:
#import "itemCell.h"
@implementation itemCell
@synthesize quantityTextField,itemTextField,priceTextField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
在filecontroller.m中,这是rootcontroller .xib的实现文件,当用户按下按钮时,我会基于此创建一个新的单元格(将其添加到数组中),然后重新加载tableview。 我没有收到任何错误,但我得到了一个普通的单元格,而不是我在IB中创建的内容。 这里是rootcontroller.m。 非常感谢您的帮助。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scrollView =(UIScrollView *)self.view;
items = [[NSMutableArray alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return [items objectAtIndex:[indexPath row]];
}
- (UITableViewCell *)initiateNewCell{
itemCell *cell = [[itemCell alloc] init];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
return cell;
}
- (IBAction)addItem:(id)sender {
//creates the new cell to be added
[items addObject:[self initiateNewCell]];
[self.tableView reloadData];