Iphone 在ios中覆盖UItable View单元格中的内容
在我的表视图单元格中,我需要在单个单元格中显示数组元素的内容,如果插入任何新内容,则不会覆盖以前的内容。以前的内容和我的新内容应按顺序显示。这是我的代码Iphone 在ios中覆盖UItable View单元格中的内容,iphone,objective-c,ios,xcode,uitableview,Iphone,Objective C,Ios,Xcode,Uitableview,在我的表视图单元格中,我需要在单个单元格中显示数组元素的内容,如果插入任何新内容,则不会覆盖以前的内容。以前的内容和我的新内容应按顺序显示。这是我的代码 - (void)viewDidLoad { [super viewDidLoad]; NSArray *arr=[NSArray arrayWithObjects:cnfqty,cnftitle,cnfrate, nil]; //cnfqty,cnftitle,cnfrate are NSString finarray=[[NSMuta
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *arr=[NSArray arrayWithObjects:cnfqty,cnftitle,cnfrate, nil];
//cnfqty,cnftitle,cnfrate are NSString
finarray=[[NSMutableArray alloc]init];
[finarray addObjectsFromArray:arr];
NSLog(@"%@",finarray);
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [finarray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
lab.text=[finarray objectAtIndex:indexPath.row];
[cell.contentView addSubview:lab];
}
// Configure the cell...
return cell;
}
我的代码面临的问题是,每个字符串都显示在每个单元格中,如果我插入任何新字符串,以前的内容就会消失,只显示新内容
为了弄清楚这个问题,我正在尝试为在线购物实施“添加到购物车”服务。根据概念,物品必须从各种产品中添加,它保存产品信息,并且必须在表视图中显示详细信息。但我不明白
请引导..提前感谢..您在
cellForRowAtIndexPath
中的代码有点不正确。在初始化之前,您应该检查单元格是否为nil。似乎你从某个地方复制了括号,但遗漏了条件
除此之外,对于被重用的单元,您没有代码来更改单元的标签。您只在新文本上设置文本。跟踪标签以重用它的一个简单方法是给它一个标签
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{ // set up brand new cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
lab.tag = 150; // some int you can keep track of, possibly make it a define.
[cell.contentView addSubview:lab];
}
// configure cell
UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:150]; // there's that tag again
cellLabel.text = [finarray objectAtIndex:indexPath.row];
return cell;
}
cellforrowatinexpath
中的代码有点不正确。在初始化之前,您应该检查单元格是否为nil。似乎你从某个地方复制了括号,但遗漏了条件
除此之外,对于被重用的单元,您没有代码来更改单元的标签。您只在新文本上设置文本。跟踪标签以重用它的一个简单方法是给它一个标签
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{ // set up brand new cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
lab.tag = 150; // some int you can keep track of, possibly make it a define.
[cell.contentView addSubview:lab];
}
// configure cell
UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:150]; // there's that tag again
cellLabel.text = [finarray objectAtIndex:indexPath.row];
return cell;
}
让你的“finarray”在每个对象中存储一个数组(我的意思是你需要一种二维数组)。在cellforrowatinexpath
中,您将通过indexPath.row
从finarray
提取数组,并在提取的数组中循环以填充内容。第一次,finarray中的每个数组将仅包含1个对象。通过更改单元格中的内容,实际上将向位于已编辑单元格索引处的数组中再添加一个对象。然后,通过重新加载数据,表将显示新添加的数据。请确保管理已编辑单元格的行高度,因为它们需要增加
- (void)viewDidLoad
{
[super viewDidLoad];
//bla bla bla whatever you had here
NSMutableArray *arr1=[NSMutableArray arrayWithObjects:cnfqty, nil];
NSMutableArray *arr2=[NSMutableArray arrayWithObjects:cnftitle, nil];
NSMutableArray *arr3=[NSMutableArray arrayWithObjects:cnfrate, nil];
NSMutableArray *arrMain=[NSMutableArray arrayWithObjects:arr1, arr2, arr3, nil];
finarray=[[NSMutableArray alloc]init];
[finarray addObjectsFromArray:arr];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//bla bla bla the code to initiate the cell
NSArray *tmpArray = [finarray objectAtIndex:indexPath.row];
for(int i = 0; i<tmpArray.count;i++){
//create the label
//set the frame, making sure that origin.y is multiplied by "i"
//set label's text as [tmpArray objectAtIndex:i]
//add the label to cell
}
return cell;
}
-(void)viewDidLoad
{
[超级视图下载];
//诸如此类诸如此类
NSMUTABLEARRY*arr1=[NSMUTABLEARRY arrayWithObjects:cnfqty,nil];
NSMUTABLEARRY*arr2=[NSMUTABLEARRY arrayWithObjects:cnftitle,nil];
NSMUTABLEARRY*arr3=[NSMUTABLEARRY arrayWithObjects:cnfrate,nil];
NSMUTABLEARRY*arrMain=[NSMUTABLEARRY数组及其对象:arr1、arr2、arr3、nil];
finarray=[[NSMutableArray alloc]init];
[finarray addObjectsFromArray:arr];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
//bla bla bla bla启动单元的代码
NSArray*tmpArray=[finarray objectAtIndex:indexPath.row];
对于(inti=0;i使您的“finarray”在每个对象中存储一个数组(我的意思是您需要一种二维数组)。在cellforrowatinexpath
中,您将通过indexPath.row
从finarray
提取数组,并在提取的数组中循环以填充您的内容。finarray中的每个数组第一次仅包含1个对象。通过更改单元格中的内容,您实际上将向位于已编辑单元格索引处的数组。然后,通过重新加载数据,表格将显示新添加的数据。请确保管理已编辑单元格的行高度,因为它们需要增加
- (void)viewDidLoad
{
[super viewDidLoad];
//bla bla bla whatever you had here
NSMutableArray *arr1=[NSMutableArray arrayWithObjects:cnfqty, nil];
NSMutableArray *arr2=[NSMutableArray arrayWithObjects:cnftitle, nil];
NSMutableArray *arr3=[NSMutableArray arrayWithObjects:cnfrate, nil];
NSMutableArray *arrMain=[NSMutableArray arrayWithObjects:arr1, arr2, arr3, nil];
finarray=[[NSMutableArray alloc]init];
[finarray addObjectsFromArray:arr];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//bla bla bla the code to initiate the cell
NSArray *tmpArray = [finarray objectAtIndex:indexPath.row];
for(int i = 0; i<tmpArray.count;i++){
//create the label
//set the frame, making sure that origin.y is multiplied by "i"
//set label's text as [tmpArray objectAtIndex:i]
//add the label to cell
}
return cell;
}
-(void)viewDidLoad
{
[超级视图下载];
//诸如此类诸如此类
NSMUTABLEARRY*arr1=[NSMUTABLEARRY arrayWithObjects:cnfqty,nil];
NSMUTABLEARRY*arr2=[NSMUTABLEARRY arrayWithObjects:cnftitle,nil];
NSMUTABLEARRY*arr3=[NSMUTABLEARRY arrayWithObjects:cnfrate,nil];
NSMUTABLEARRY*arrMain=[NSMUTABLEARRY数组及其对象:arr1、arr2、arr3、nil];
finarray=[[NSMutableArray alloc]init];
[finarray addObjectsFromArray:arr];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
//bla bla bla bla启动单元的代码
NSArray*tmpArray=[finarray objectAtIndex:indexPath.row];
for(int i=0;i不确定您的cellForRowAtIndexPath是否正确。看起来您没有检查cell是否为nil。如果您是(在括号内),那么您似乎只更新if(cell==nil)语句中的lab.text
它可能更像以下内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UILabel *lab;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
lab.tag = 1; // Really any arbitraty number
[cell.contentView addSubview:lab];
}else{
lab = [cell.contentView viewWithTag:1]
}
// Configure the cell...
lab.text=[finarray objectAtIndex:indexPath.row];
return cell;
}
不确定您的cellForRowAtIndexPath是否正确。看起来您没有检查cell是否为nil。如果您是(不知何故在括号内),则您似乎只是在更新if(cell==nil)语句中的lab.text
它可能更像以下内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UILabel *lab;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
lab.tag = 1; // Really any arbitraty number
[cell.contentView addSubview:lab];
}else{
lab = [cell.contentView viewWithTag:1]
}
// Configure the cell...
lab.text=[finarray objectAtIndex:indexPath.row];
return cell;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell *cell = (UITableViewCell*)[self.YourTableName dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
return cell;
}
使用ReusablecellIdentifier nil使其正常工作
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell *cell = (UITableViewCell*)[self.YourTableName dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
return cell;
}
使用ReusablecellIdentifier nil使其正常工作
我在另一个例子中尝试了它,给出了如果(cell==nil)单元格图像被重复,并且图像从我的json文件加载。因此,我没有使用条件。您需要查看我的答案,并将其与您尝试的结果进行比较,以了解您尝试的结果不正确的原因。请求单元格时可能发生两种情况:1)创建一个全新的,2)重用现有的一个。然后,可以填充视图。将此函数复制/粘贴到您的上,它应该可以正常工作。同样的问题也会发生。所有内容都显示在每个单元格中,并且警告显示为不可比较的指针类型请参阅我的编辑以修复该警告,视图只需要是c