数组在表格视图单元格中显示重复的对象 [英] Array displaying repeated object in table view cell
本文介绍了数组在表格视图单元格中显示重复的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的NSArray大小为11,如下所示.
I am having a NSArray of size 11 as it formed below.
labels = [NSMutableArray arrayWithObjects:@"DIN#",@品牌名称",@全名",@力量",@药物类型",@"Presciption ID",@下一个剂量",@所需剂量",@"ada",@"dasdada",@"dasdasad",nil];
但是当我在tableview单元格中显示此数组时,使用此代码.
but when i am displaying this array in tableview cell Using this code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dueueReusableCellWithIdentifier:CellIdentifier];
UILabel* nameLabel = nil;
if(cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
nameLabel.text =[labels objectAtIndex:indexPath.row];
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.numberOfLines =0;
[nameLabel sizeToFit];
CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame =nameLabel.frame;
newFrame.size.height =expectedlabelsize.height;
[cell.contentView addSubview: nameLabel];
return cell;}
O/P如下.
DIN
品牌名称
全名
强度
药物类型
沉淀ID
下一剂
所需剂量
DIN
品牌名称
全名
再次查看所需剂量,显示,已经显示的DIN,品牌名称,全名
推荐答案
使用 viewWithTag
方法,而我在以下方法中使用了它.尝试这样.我认为这将对您有所帮助.
Use viewWithTag
method and i have used it following method. Try like this. I think it will be helpful to you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myCell";
UILabel* nameLabel = nil;
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
[titleLbl removeFromSuperview];
}
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
nameLabel.text = [labels objectAtIndex:indexPath.row];
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.tag =1;
nameLabel.numberOfLines =0;
[nameLabel sizeToFit];
CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame =nameLabel.frame;
newFrame.size.height =expectedlabelsize.height;
[cell.contentView addSubview: nameLabel];
return cell;
}
这篇关于数组在表格视图单元格中显示重复的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文