This problem taking more time. i am just adding set of array values to table view array contains totally 12 values but it is just showing 3 values. if i am change row height more it is just displaying 3 values. if i reduce row height it showing all values so any one can help me how i can show all array value but my row height should be more than 100.
并且重要的因素是如果我在cell.textlabel中打印数组它打印所有值但我想在动态uilabel中打印数组值所以我该怎么做?
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [name count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=@"ViewProfileCell";
UILabel *lab;
ViewProfileCell *cell=(ViewProfileCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(!cell)
{
NSArray *nibofviewProfile=[[NSBundle mainBundle]loadNibNamed:@"ViewProfileCell" owner:self options:Nil];
cell=[nibofviewProfile objectAtIndex:0];
lab =[[UILabel alloc]init];
lab.frame=CGRectMake(80, 10, 30, 50);
[cell.contentView addSubview:lab];
}
lab.text=[name objectAtIndex:indexPath.row];
return cell;
}
这是我的代码,如果有人可以为此提供解决方案,我将非常高兴
答案 0 :(得分:0)
你应该在if(){}之后问你的手机是否有UILabel,因为UILabel * lab对于新的细胞而言不会是零,而不是重复使用。
static NSString * cellidentifier = @“ViewProfileCell”;
ViewProfileCell *cell=(ViewProfileCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(!cell)
{
NSArray *nibofviewProfile=[[NSBundle mainBundle]loadNibNamed:@"ViewProfileCell" owner:self options:Nil];
cell=[nibofviewProfile objectAtIndex:0];
UILabel *lab =[[UILabel alloc]init];
lab.frame=CGRectMake(80, 10, 30, 50);
lab.tag = 123;
[cell.contentView addSubview:lab];
}
UILabel *lab = [cell.contentView viewWithTag:123]; lab.text=[name objectAtIndex:indexPath.row];
建议:
将属性声明添加到ViewProfileCell类;
@property(nonatomic,strong,readonly)UILabel * nameLabel;
添加合成。
实施懒惰的getter:
- (UILabel *)nameLabel
{
if (_nameLabel) return _nameLabel;
_nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 10, 30, 50)];
[self.contentView addSubview:_nameLabel;
return _nameLabel;
}
所以你的代码将是
static NSString *cellidentifier=@"ViewProfileCell";
ViewProfileCell *cell=(ViewProfileCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(!cell)
{
NSArray *nibofviewProfile=[[NSBundle mainBundle]loadNibNamed:@"ViewProfileCell" owner:self options:Nil];
cell=[nibofviewProfile objectAtIndex:0];
}
cell.nameLabel.text=[name objectAtIndex:indexPath.row];