UITableViewCell中的UILabel可以创建自定义单元,而无需创建单独的类 [英] UILabel in UITableViewCell to make a customised cell without making separate class

查看:41
本文介绍了UITableViewCell中的UILabel可以创建自定义单元,而无需创建单独的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表格视图单元格中,我需要在一个单元格中显示数组元素的内容,如果我插入任何新内容,则先前的内容将不会被覆盖.先前的内容和我的新内容应按顺序显示这是我的代码

In my table view cell i need to display the contents of my array elements in a single cell and if i insert any new contents the previous contents are not to be overwritten.The previous content and my new content should be displayed in order.Here is my code

- (void)viewDidLoad
{
[super viewDidLoad];




NSMutableArray *arrMain=[NSMutableArray arrayWithObjects: cnfqty, cnftitle, cnfrate, nil];

finarray=[[NSMutableArray alloc]init];
[finarray addObjectsFromArray:arrMain];
   NSLog(@"%@",finarray);


}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

 #pragma mark - Table view data source

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {

// Return the number of sections.
return 1;
}

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {

return 150.0;

 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 // Return the number of rows in the section.
return ( [finarray count] / 3 ); 
}


 - (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

UILabel *tempLabel;
//UIImage *tempImage;

/* Create UI elements - Customize as you want your text to appear */

CGRect myFirstLabel = CGRectMake (65, 10, 200, 15);
CGRect mySecondLabel = CGRectMake (65, 25, 200, 15);
CGRect myThirdLabel = CGRectMake (65, 40, 200, 15);
 //  CGRect myImage = CGRectMake (10, 10, 50, 50);

//Initialize first label
tempLabel = [[UILabel alloc] initWithFrame: myFirstLabel];
tempLabel.tag = 1;
tempLabel.font = [UIFont boldSystemFontOfSize:12];
tempLabel.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview: tempLabel];

//Initialize second label
tempLabel = [[UILabel alloc] initWithFrame: mySecondLabel];
tempLabel.tag = 2;
tempLabel.font = [UIFont boldSystemFontOfSize:12];
tempLabel.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview: tempLabel];

//Initialize third label
tempLabel = [[UILabel alloc] initWithFrame: myThirdLabel];
tempLabel.tag = 3;
tempLabel.font = [UIFont boldSystemFontOfSize:12];
tempLabel.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview: tempLabel];

//Initialize your picture
//imgTemp = [[UIImageView alloc] initWithFrame: myImage];
//imgTemp.tag = 4;
//[cell.contentView addSubview: tempImage];

return cell;
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"aCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [self getCellContentView: CellIdentifier];
}

//This will create the actual UI elements
UILabel *tempLabel1 = (UILabel *)[cell viewWithTag: 1];
UILabel *tempLabel2 = (UILabel *)[cell viewWithTag: 2];
UILabel *tempLabel3 = (UILabel *)[cell viewWithTag: 3];
//UIImageView *tempImage = (UIImageView *)[cell viewWithTag: 4];

// Populate the labels
NSString *firstText = [NSString stringWithString: [finarray objectAtIndex: 3*(indexPath.row)]];
NSString *secondText = [NSString stringWithString: [finarray objectAtIndex: 3*(indexPath.row) + 1]];
NSString *thirdText = [NSString stringWithString: [finarray objectAtIndex: 3*(indexPath.row) + 2]];

// Populate the picture
//UIImage *picture = [[UIImage alloc] initWithData:theObjectToPutInCell.picture];

tempLabel1.text = firstText;
tempLabel2.text = secondText;
tempLabel3.text = thirdText;

 // tempImage.image = picture;

return cell;  


}
}

@end

我的代码面临的问题是每个字符串都显示在每个单元格中,如果我插入任何新字符串,以前的内容就会消失,而只会显示新的内容.

The problem which i'm facing with my code is each string is displayed in each cell and if i insert any new string previous contents are gone and only new contents are displayed.

要对此问题有个清晰的了解,我正在尝试为在线购物实施添加到购物车"服务.根据概念,必须从各种产品中添加商品,这样才能保存产品信息并必须显示详细信息在表格视图中.但是我不明白.

To get clear idea about this question i'm trying to implement "add to cart" service for online shopping.As per concept the items have to be added from various products and it saves the product info and have to display the details in Table view.But i'm not getting it..

请亲切的向导..谢谢.

Kindly Guide please..Thanks in advance..

我的输出就像这张第一张图片

My output is like this first image

但是我需要显示为第二张图片,这是在Android中完成的.

But i need to get display as second image which done in Android..

推荐答案

最后,我通过以下代码得到了

Finally i got it by following code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"aCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *label1=[[UILabel alloc]init];
label1.frame=.....;
................
[cell.contentView addSubView: label1];

UILabel *label2=[[UILabel alloc]init];
label2.frame=.....;
................
[cell.contentView addSubView: label2];

UILabel *label3=[[UILabel alloc]init];
label3.frame=.....;
................
[cell.contentView addSubView: label3];
}

标签文本是从这样的单个Array元素分配的

The text to the labels are assigned from individual Array element like this

NSMutableArray *label1Array=[NSMutableArray arrayWithObjects:@"",@"",nil];
NSMutableArray *label2Array=[NSMutableArray arrayWithObjects:@"",@"",nil];
NSMutableArray *label3Array=[NSMutableArray arrayWithObjects:@"",@"",nil];

这篇关于UITableViewCell中的UILabel可以创建自定义单元,而无需创建单独的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆