Ios 2具有不同自定义单元格的UITableView
我需要在一个视图中使用两个tableview。我还需要用自定义单元格创建这些表 我尝试了一些东西,但在我表格的单元格中,我甚至看不到简单的字符串 这里怎么了 这是我的最新代码 我仍然有问题,我正在接受无效参数异常[LastVisitOrder superview]:发送到实例的无法识别的选择器。因为我已将uiview更改为uiview控制器Ios 2具有不同自定义单元格的UITableView,ios,uitableview,custom-cell,Ios,Uitableview,Custom Cell,我需要在一个视图中使用两个tableview。我还需要用自定义单元格创建这些表 我尝试了一些东西,但在我表格的单元格中,我甚至看不到简单的字符串 这里怎么了 这是我的最新代码 我仍然有问题,我正在接受无效参数异常[LastVisitOrder superview]:发送到实例的无法识别的选择器。因为我已将uiview更改为uiview控制器 - (id)initWithFrame:(CGRect)frame { self.view = [super initWithFrame:frame];
- (id)initWithFrame:(CGRect)frame
{
self.view = [super initWithFrame:frame];
if (self) {
self.view.frame = CGRectMake(0, 0, 1024, 768);
[self.view setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];
UIView *popup = [[UIView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-860)/2,(self.view.frame.size.height-570)/2, 860, 570)];
[popup setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"popup_bckgrnd"]]];
[self.view addSubview:popup];
UILabel *customerName = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 300, 30)];
[customerName setBackgroundColor:[UIColor clearColor]];
[customerName setText:@"Beşiktaş Eczanesi"];
[customerName setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18]];
[popup addSubview:customerName];
UILabel *customerDetail = [[UILabel alloc]initWithFrame:CGRectMake(50, 70, 300, 30)];
[customerDetail setBackgroundColor:[UIColor clearColor]];
[customerDetail setText:@"Tel: 231 213 23 23 Bla bla cad. Bla bla Sok. Bla bla Apt. No:Bla Blastan/Bistanbul"];
[customerDetail setFont:[UIFont fontWithName:@"Arial" size:14]];
[popup addSubview:customerDetail];
UIButton *close = [[UIButton alloc]initWithFrame:CGRectMake(780, 65, 19, 19)];
[close setBackgroundImage:[UIImage imageNamed:@"kapat"] forState:UIControlStateNormal];
[close addTarget:self action:@selector(closePopUp:) forControlEvents:UIControlEventTouchUpInside];
[popup addSubview:close];
UIView *titleLine = [[UIView alloc]initWithFrame:CGRectMake(35, 100, 790, 2)];
[titleLine setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"baslik_cizgi"]]];
[popup addSubview:titleLine];
dateTable = [[UITableView alloc]initWithFrame:CGRectMake(48.5, 165, 200, 345)];
dateTable.layer.cornerRadius = 10;
[dateTable setBackgroundColor:[UIColor clearColor]];
[popup addSubview:dateTable];
dateTable.delegate=self;
dateTable.dataSource=self;
UIImageView *visitDatesLabel = [[UIImageView alloc]initWithFrame:CGRectMake(70, 122, 148, 30)];
[visitDatesLabel setImage:[UIImage imageNamed:@"en_son_ziyaret"]];
[popup addSubview:visitDatesLabel];
UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(49.5, 160, 200, 2)];
[line setImage:[UIImage imageNamed:@"ust_cizgi"]];
[popup addSubview:line];
UIImageView *line2 = [[UIImageView alloc]initWithFrame:CGRectMake(270.5, 160, 540, 2)];
[line2 setImage:[UIImage imageNamed:@"ust_cizgi"]];
[popup addSubview:line2];
UIImageView *productsDatesLabel = [[UIImageView alloc]initWithFrame:CGRectMake(300, 122, 102, 29)];
[productsDatesLabel setImage:[UIImage imageNamed:@"urunler"]];
[popup addSubview:productsDatesLabel];
UIImageView *verticalLine = [[UIImageView alloc]initWithFrame:CGRectMake(430, 115, 2, 45)];
[verticalLine setImage:[UIImage imageNamed:@"dikey_cizgi"]];
[popup addSubview:verticalLine];
UILabel *firstMonth = [[UILabel alloc]initWithFrame:CGRectMake(455, 126, 148, 30)];
[firstMonth setBackgroundColor:[UIColor clearColor]];
[firstMonth setText:@"OCAK"];
[firstMonth setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[firstMonth setTextColor:[UIColor grayColor]];
[popup addSubview:firstMonth];
UILabel *secondMonth = [[UILabel alloc]initWithFrame:CGRectMake(455+75, 126, 148, 30)];
[secondMonth setBackgroundColor:[UIColor clearColor]];
[secondMonth setText:@"ŞUBAT"];
[secondMonth setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[secondMonth setTextColor:[UIColor grayColor]];
[popup addSubview:secondMonth];
UILabel *thirdMonth = [[UILabel alloc]initWithFrame:CGRectMake(455+75+75, 126, 148, 30)];
[thirdMonth setBackgroundColor:[UIColor clearColor]];
[thirdMonth setText:@"MART"];
[thirdMonth setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[thirdMonth setTextColor:[UIColor grayColor]];
[popup addSubview:thirdMonth];
UILabel *rangeMonth = [[UILabel alloc]initWithFrame:CGRectMake(455+75+75+100, 126, 148, 30)];
[rangeMonth setBackgroundColor:[UIColor clearColor]];
[rangeMonth setText:@"OCAK-MART"];
[rangeMonth setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[rangeMonth setTextColor:[UIColor grayColor]];
[popup addSubview:rangeMonth];
UIImageView *verticalLine2 = [[UIImageView alloc]initWithFrame:CGRectMake(510, 115, 2, 45)];
[verticalLine2 setImage:[UIImage imageNamed:@"dikey_cizgi"]];
[popup addSubview:verticalLine2];
UIImageView *verticalLine3 = [[UIImageView alloc]initWithFrame:CGRectMake(590, 115, 2, 45)];
[verticalLine3 setImage:[UIImage imageNamed:@"dikey_cizgi"]];
[popup addSubview:verticalLine3];
UIImageView *verticalLine4 = [[UIImageView alloc]initWithFrame:CGRectMake(670, 115, 2, 45)];
[verticalLine4 setImage:[UIImage imageNamed:@"dikey_cizgi"]];
[popup addSubview:verticalLine4];
medSalesTable = [[UITableView alloc]initWithFrame:CGRectMake(270.5, 165, 540, 345)];
medSalesTable.layer.cornerRadius = 10;
[medSalesTable setBackgroundColor:[UIColor clearColor]];
[popup addSubview:medSalesTable];
medSalesTable.delegate=self;
medSalesTable.dataSource=self;
}
return self;
}
-(void) closePopUp:(id)sender{
[self.view removeFromSuperview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@“cellforrowatinexpath”)
使用
cell=[[NSBundle mainBundle]loadNibNamed:@“VisitDateCell”所有者:自我选项:nil]objectAtIndex:0];
而不是cell=[[[VisitDateCell alloc]initWithFrame:CGRectZero]autorelease];
然后使用cell.textlab.text=@“您的文本”
更新!
要访问可变日期,您需要在VisitDateCell.h
中全局创建它。例如:
@属性(非原子,强)UILabel*日期;
然后使用TableView实现访问使用:
(VisitDateCell*)cell.date=@“您的文本”
您不需要在自定义方法setText中使用此代码。只需在cellForRows
中使用此代码即可
希望它能有所帮助!在您的
数据源方法中,您需要区分这两个表
//cellForRowAtIndexPath
if (tableView == medSalesTable) {
// configure the first table
}
else {
// configure the second table
// use a different cell identifier
}
如有必要,请确保在numberOfRowsInSection
和NumberOfSectionsTableView
中也执行此操作。这假设您将两个表视图datasource
属性都设置为控制器。这是一种优雅的方式,我一直在使用。。
- (id)initWithFrame:(CGRect)frame
{
self.view = [super initWithFrame:frame];
if (self) {
self.view.frame = CGRectMake(0, 0, 1024, 768);
[self.view setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];
UIView *popup = [[UIView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-860)/2,(self.view.frame.size.height-570)/2, 860, 570)];
[popup setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"popup_bckgrnd"]]];
[self.view addSubview:popup];
UILabel *customerName = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 300, 30)];
[customerName setBackgroundColor:[UIColor clearColor]];
[customerName setText:@"Beşiktaş Eczanesi"];
[customerName setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18]];
[popup addSubview:customerName];
UILabel *customerDetail = [[UILabel alloc]initWithFrame:CGRectMake(50, 70, 300, 30)];
[customerDetail setBackgroundColor:[UIColor clearColor]];
[customerDetail setText:@"Tel: 231 213 23 23 Bla bla cad. Bla bla Sok. Bla bla Apt. No:Bla Blastan/Bistanbul"];
[customerDetail setFont:[UIFont fontWithName:@"Arial" size:14]];
[popup addSubview:customerDetail];
UIButton *close = [[UIButton alloc]initWithFrame:CGRectMake(780, 65, 19, 19)];
[close setBackgroundImage:[UIImage imageNamed:@"kapat"] forState:UIControlStateNormal];
[close addTarget:self action:@selector(closePopUp:) forControlEvents:UIControlEventTouchUpInside];
[popup addSubview:close];
UIView *titleLine = [[UIView alloc]initWithFrame:CGRectMake(35, 100, 790, 2)];
[titleLine setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"baslik_cizgi"]]];
[popup addSubview:titleLine];
dateTable = [[UITableView alloc]initWithFrame:CGRectMake(48.5, 165, 200, 345)];
dateTable.layer.cornerRadius = 10;
[dateTable setBackgroundColor:[UIColor clearColor]];
[popup addSubview:dateTable];
dateTable.delegate=self;
dateTable.dataSource=self;
UIImageView *visitDatesLabel = [[UIImageView alloc]initWithFrame:CGRectMake(70, 122, 148, 30)];
[visitDatesLabel setImage:[UIImage imageNamed:@"en_son_ziyaret"]];
[popup addSubview:visitDatesLabel];
UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(49.5, 160, 200, 2)];
[line setImage:[UIImage imageNamed:@"ust_cizgi"]];
[popup addSubview:line];
UIImageView *line2 = [[UIImageView alloc]initWithFrame:CGRectMake(270.5, 160, 540, 2)];
[line2 setImage:[UIImage imageNamed:@"ust_cizgi"]];
[popup addSubview:line2];
UIImageView *productsDatesLabel = [[UIImageView alloc]initWithFrame:CGRectMake(300, 122, 102, 29)];
[productsDatesLabel setImage:[UIImage imageNamed:@"urunler"]];
[popup addSubview:productsDatesLabel];
UIImageView *verticalLine = [[UIImageView alloc]initWithFrame:CGRectMake(430, 115, 2, 45)];
[verticalLine setImage:[UIImage imageNamed:@"dikey_cizgi"]];
[popup addSubview:verticalLine];
UILabel *firstMonth = [[UILabel alloc]initWithFrame:CGRectMake(455, 126, 148, 30)];
[firstMonth setBackgroundColor:[UIColor clearColor]];
[firstMonth setText:@"OCAK"];
[firstMonth setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[firstMonth setTextColor:[UIColor grayColor]];
[popup addSubview:firstMonth];
UILabel *secondMonth = [[UILabel alloc]initWithFrame:CGRectMake(455+75, 126, 148, 30)];
[secondMonth setBackgroundColor:[UIColor clearColor]];
[secondMonth setText:@"ŞUBAT"];
[secondMonth setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[secondMonth setTextColor:[UIColor grayColor]];
[popup addSubview:secondMonth];
UILabel *thirdMonth = [[UILabel alloc]initWithFrame:CGRectMake(455+75+75, 126, 148, 30)];
[thirdMonth setBackgroundColor:[UIColor clearColor]];
[thirdMonth setText:@"MART"];
[thirdMonth setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[thirdMonth setTextColor:[UIColor grayColor]];
[popup addSubview:thirdMonth];
UILabel *rangeMonth = [[UILabel alloc]initWithFrame:CGRectMake(455+75+75+100, 126, 148, 30)];
[rangeMonth setBackgroundColor:[UIColor clearColor]];
[rangeMonth setText:@"OCAK-MART"];
[rangeMonth setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[rangeMonth setTextColor:[UIColor grayColor]];
[popup addSubview:rangeMonth];
UIImageView *verticalLine2 = [[UIImageView alloc]initWithFrame:CGRectMake(510, 115, 2, 45)];
[verticalLine2 setImage:[UIImage imageNamed:@"dikey_cizgi"]];
[popup addSubview:verticalLine2];
UIImageView *verticalLine3 = [[UIImageView alloc]initWithFrame:CGRectMake(590, 115, 2, 45)];
[verticalLine3 setImage:[UIImage imageNamed:@"dikey_cizgi"]];
[popup addSubview:verticalLine3];
UIImageView *verticalLine4 = [[UIImageView alloc]initWithFrame:CGRectMake(670, 115, 2, 45)];
[verticalLine4 setImage:[UIImage imageNamed:@"dikey_cizgi"]];
[popup addSubview:verticalLine4];
medSalesTable = [[UITableView alloc]initWithFrame:CGRectMake(270.5, 165, 540, 345)];
medSalesTable.layer.cornerRadius = 10;
[medSalesTable setBackgroundColor:[UIColor clearColor]];
[popup addSubview:medSalesTable];
medSalesTable.delegate=self;
medSalesTable.dataSource=self;
}
return self;
}
-(void) closePopUp:(id)sender{
[self.view removeFromSuperview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这里只处理创建多个自定义单元格,没有计算单元格高度的代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/*
Call a function to create all custom cells.
Send the tableview and the indexPath to this function.
So, your code will be clean and easy to read an maintenance =D
DON'T forget to change the height of each cell
*/
if (indexPath.row < 3)
return [self createACustomCell1:tableView indexPath:indexPath];
else
return [self createACustomCell2:tableView indexPath:indexPath];
}
//*************
// Create CUSTOM CELL 2
//*************
-(UITableViewCell *)createACustomCell1:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{
static NSString *CUSTOMCELL_1 = @"CUSTOMCELL_1";
CustomCell_1 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1];
if (!cell){
[anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_1
bundle:nil] forCellReuseIdentifier:CUSTOMCELL_1];
cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1];
}
// Cell customization above
return cell;
}
//*************
// Create CUSTOM CELL 2
//*************
-(UITableViewCell *)createACustomCell2:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{
static NSString *CUSTOMCELL_2 = @"CUSTOMCELL_2";
CustomCell_2 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2];
if (!cell){
[anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_2
bundle:nil] forCellReuseIdentifier:CUSTOMCELL_2];
cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2];
}
// Cell customization above
return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
/*
调用函数以创建所有自定义单元格。
将tableview和indexPath发送到此函数。
因此,您的代码将是干净且易于阅读的维护=D
不要忘记更改每个单元格的高度
*/
if(indexPath.row<3)
返回[self-createACustomCell1:tableView indexPath:indexPath];
其他的
返回[self-createACustomCell2:tableView indexPath:indexPath];
}
//*************
//创建自定义单元格2
//*************
-(UITableViewCell*)createACustomCell1:(UITableView*)anTableView indexPath:(NSIndexPath*)indexPath{
静态NSString*CUSTOMCELL_1=@“CUSTOMCELL_1”;
CustomCell_1*cell=[anTableView dequeueReusableCellWithIdentifier:CustomCell_1];
如果(!单元格){
[anTableView注册表项nb:[UINib nibWithNibName:CUSTOMCELL_1
bundle:nil]forCellReuseIdentifier:CUSTOMCELL_1];
cell=[anTableView出列可重用CellWithIdentifier:CUSTOMCELL_1];
}
//上面的单元格自定义
返回单元;
}
//*************
//创建自定义单元格2
//*************
-(UITableViewCell*)createACustomCell2:(UITableView*)anTableView indexPath:(NSIndexPath*)indexPath{
静态NSString*CUSTOMCELL_2=@“CUSTOMCELL_2”;
CustomCell_2*cell=[anTableView dequeueReusableCellWithIdentifier:CustomCell_2];
如果(!单元格){
[anTableView注册表项nb:[UINib nibWithNibName:CUSTOMCELL_2
bundle:nil]强制重用标识符:CUSTOMCELL_2];
cell=[anTableView出列可重用CellWithIdentifier:CUSTOMCELL_2];
}
//上面的单元格自定义
返回单元;
}
但我无法将“date”作为cell.date.text访问这里有几点错误,第一点是LastVisitOrder是一个视图,而视图不应该是表视图的数据源(假设您曾经设置过数据源,是吗?)。它应该是uiview控制器而不是uiview吗?是的,并确保将其设置为表的数据源。是的,您应该设置委托
和数据源
。我设置了,但仍然没有区别,表上没有显示任何内容table@erdemgc使用cell=[[NSBundle mainBundle]loadNibNamed:@“VisitDateCell”所有者:自选项:nil]objectAtIndex:0];