我有一个tableview
,我想创建两个自定义单元格。我为UITableViewcell
创建了两个自定义类。如果我一次创建一个类单元格。然后它正常工作。但如果我同时创造两者。它显示例外情况。
代码:
- (void)viewDidLoad
{
[super viewDidLoad];
check=@"a";
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewWillLayoutSubviews
{
tb=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
tb.delegate=self;
tb.dataSource=self;
[self.view addSubview:tb];
[tb registerClass:[imgTableViewCell class] forCellReuseIdentifier:CI];
[tb registerClass:[_TableViewCell class] forCellReuseIdentifier:CII]; // I think, problem is here.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 6;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([check isEqualToString:@"a"])
{
IMGCELL=[tableView dequeueReusableCellWithIdentifier:CI];
if(!IMGCELL)
{
IMGCELL=[[imgTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CI];
}
IMGCELL.feeds.text=@"HEHEHE";
return IMGCELL;
}
else
if([check isEqualToString:@"b"])
{
CELL=[tableView dequeueReusableCellWithIdentifier:CII];
if(!CELL)
{
CELL=[[_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CII];
}
CELL.IMGV.image=[UIImage imageNamed:@"band"];
return CELL;
}
return nil;
}
例外:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TableViewCell feeds]: unrecognized selector sent to instance 0x8d5c660'
问题:问题是,我正在向表视图注册两个类。是否有unregister
课程的方法。
答案 0 :(得分:0)
像往常一样,定义tableview的以下数据源方法,以便为tableview加载一些数据。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cellForRowAtIndexPath方法中的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}
containerObject = [objectCollection objectAtIndex:indexPath.row];
[[cell textLabel] setText:containerObject.store];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
} else {
static NSString* cellIdentifier1 = @"FeatureCell";
FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
if (cell1 == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil];
cell1 = (FeatureCell*)[nib objectAtIndex:0];
}
cell1.img1.image = [UIImage imageNamed:@"shower.png"];
cell1.img2.image = [UIImage imageNamed:@"parking.png"];
return cell1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
//We are doing some rough calculation for loading different xibs or different rows
if (indexPath.row % 2 == 0) {
BlogpostCell *cell = (BlogpostCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"BlogpostCell" owner:self options:nil];
//Assign the instance of the tableview cell that we have created in the custom cell
cell = blogPostCell;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.postType.text = @"Cook said Apple has acquired 23 companies in the last 16 months and remained on the lookout for interesting technology and companies.Apple is not in a race to acquire the most companies or to spend the most money, but that doesn't mean we won't buy a huge company tomorrow afternoon, he said.And he warned shareholders not to focus too narrowly on short-term gains.If you're in Apple for only a week ... or two months, I would encourage you not to invest in Apple,he said.We are here for the long term";
return cell;
}
else if (indexPath.row % 3 == 0) {
MicroblogCell *cell = (MicroblogCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"MicroblogCell" owner:self options:nil];
cell = microBlogCell;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.postContent.text = @"After trying to penetrate the mid-range mobile segment with the outdated iPhone 4, Apple India has decided to offer buyback discounts on the MacBook if you are returning your old Apple laptop";
return cell;
}
else {
ShareCell *cell = (ShareCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"ShareCell" owner:self options:nil];
cell = shareCell;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.postContent.text = @"After smartphones, now almost every tech company is jumping onto smart wearable devices, many of them focusing on health, nutrition and fitness.";
return cell;
}
}
在上面的代码中,我们正在寻找一个特定的行索引来加载相应的单元格并返回相同的单元格。 我们还可以通过计算不同内容的动态高度并显示相同的
来进一步自定义单元格答案 1 :(得分:0)
我认为this is为tableView创建自定义单元格的麻烦方式,我的示例包括单元格内的按钮操作委托,尝试使用它,我希望你解决问题,这个实现对于你在未来的项目中。 例如,您的表格视图委托方法应如下所示>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
if(!cell){
cell = (CustomTableViewCell *)[CustomTableViewCell cell];
}
cell.tapDelegate = self;
[cell fillCellWith:indexPath andSomeThingElse:@"test"];
return cell;
}
else
{
CistomTableViewCell2 * cell = [tableView dequeueReusableCellWithIdentifier:@"CistomTableViewCell2"];
if(!cell){
cell = (CistomTableViewCell2 *)[CistomTableViewCell2 cell];
}
return cell;
}
如果您对自定义此实现有疑问,我可以帮助您......