自定义单元格未在 iOS 中显示完整的数组数据


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;

}

这是我的代码,如果有人能为此提供解决方案,我将非常高兴

您应该在 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(非原子、强、只读( UILabel *nameLabel;

添加合成。

并实现惰性吸气器:

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

最新更新