gpt4 book ai didi

iphone - UITableViewCell 中的 UILabel 无需创建单独的类即可创建自定义单元格

转载 作者:行者123 更新时间:2023-11-28 18:40:25 25 4
gpt4 key购买 nike

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

- (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

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

为了清楚地了解这个问题,我正在尝试为在线购物实现“添加到购物车”服务。根据概念,必须从各种产品中添加项目,并且它保存产品信息并且必须显示详细信息在 TableView 中。但我不明白..

请指导..提前致谢..

我的输出就像第一张图片

enter image description here

但我需要显示为在 Android 中完成的第二张图片.. enter image description here

最佳答案

这里有一些你可以尝试的东西:

创建一个像这样的函数来创建所有需要的图形元素:

// Cell appearence editing
- (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: mySecondLabel];
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;
}

然后,在您的 cellForRowAtIndexPath 中,调用“设计函数”并用您的内容填充它:

- (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 stringAtIndex: 3*(indexPath.row)]];
NSString *secondText = [NSString stringWithString: [finarray stringAtIndex: 3*(indexPath.row) + 1]];
NSString *thirdText = [NSString stringWithString: [finarray stringAtIndex: 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;

}

如果有帮助请告诉我!

关于iphone - UITableViewCell 中的 UILabel 无需创建单独的类即可创建自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11669766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com