0
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;


}

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

4

1 回答 1

0

您应该在 if () {} 之后向您的单元格询问 UILabel,因为 UILabel *lab 不会仅针对新单元格,而不是重复使用的单元格为零。

静态 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;

添加合成。

并实现惰性吸气剂:

- (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];
于 2013-08-03T09:03:08.730 回答