Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UITableView滚动时感觉杂乱无章_Objective C_Ios_Cocoa Touch_Uitableview - Fatal编程技术网

Objective c UITableView滚动时感觉杂乱无章

Objective c UITableView滚动时感觉杂乱无章,objective-c,ios,cocoa-touch,uitableview,Objective C,Ios,Cocoa Touch,Uitableview,我有一个tableView,其中我将UILabel作为cellForRowAtIndexPath方法中的子视图添加到CellContentView,如下所示: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"ReuseCell"; UITableViewCell *c

我有一个tableView,其中我将UILabel作为cellForRowAtIndexPath方法中的子视图添加到CellContentView,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"ReuseCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil ){
    cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Code to remove labels overlay
for(UIView* view in cell.contentView.subviews){
    if ([view isKindOfClass:[UIView class]]) {
        [view removeFromSuperview];
    }
}
if ([indexPath row]<[itemsArray count]){
    UILabel* label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,18,280,10)];
    label1.text = [NSString stringWithFormat:@"Description %@",[[itemsArray objectAtIndex:[indexPath row]] valueForKey:@"Desc"]];
            label1.font = [UIFont fontWithName:@"Helvetica" size:14];
    [cell.contentView addSubview:label1];
    [label1 release];

    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10,25,280,10)];
    label2.text = [NSString stringWithFormat:@"Price %@",[[itemsArray objectAtIndex:[indexPath row]] valueForKey:@"Price"]];
    label2.font = [UIFont fontWithName:@"Helvetica" size:14];
    [cell.contentView addSubview:label2];
    [label2 release];

    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(10,40,290,11)];
    label3.text = [NSString stringWithFormat:@"Loc %@",[[itemsArray objectAtIndex:[indexPath row]] valueForKey:@"Loc"]];
    label3.font = [UIFont fontWithName:@"Helvetica" size:14];
    [cell.contentView addSubview:label3];
    [label3 release];
}
return cell;

这就是为什么我的滚动不是那么顺利,如果有许多行显示。如何使滚动顺畅?

不要删除标签:重新使用它们。如果给每个文件都添加了一个
标记
,则可以使用内容视图的
-viewWithTag:
检索它们,并只需更改它们的文本即可

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ReuseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *label1;
    UILabel *label2;
    UILabel *label3;
    if( cell == nil )
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,18,280,10)];
        label1.font = [UIFont fontWithName:@"Helvetica" size:14];
        label1.tag = 1;
        [cell.contentView addSubview:label1];
        [label1 release];

        label2 = [[UILabel alloc] initWithFrame:CGRectMake(10,25,280,10)];
        label2.font = [UIFont fontWithName:@"Helvetica" size:14];
        label2.tag = 2;
        [cell.contentView addSubview:label2];
        [label2 release];

        label3 = [[UILabel alloc] initWithFrame:CGRectMake(10,40,290,11)];
        label3.font = [UIFont fontWithName:@"Helvetica" size:14];
        label3.tag = 3;
        [cell.contentView addSubview:label3];
        [label3 release];
    }
    else
    {
        // retrieve the labels using the tags you defined earlier

        label1 = (UILabel *)[cell.contentView viewWithTag:1];
        label2 = (UILabel *)[cell.contentView viewWithTag:2];
        label3 = (UILabel *)[cell.contentView viewWithTag:3];
    }

    if ( [indexPath row] < [itemsArray count] )
    {
        NSDictionary *item = [itemsArray objectAtIndex:[indexPath row]];
        label1.text = [NSString stringWithFormat:@"Description %@", [item valueForKey:@"Desc"]];
        label2.text = [NSString stringWithFormat:@"Price %@", [item valueForKey:@"Price"]];
        label3.text = [NSString stringWithFormat:@"Loc %@", [item valueForKey:@"Loc"]];
    }

    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“ReuseCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel*label1;
UILabel*label2;
UILabel*label3;
如果(单元格==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier]自动释放];
label1=[[UILabel alloc]initWithFrame:CGRectMake(10,18280,10)];
label1.font=[UIFont fontWithName:@“Helvetica”大小:14];
标签1.tag=1;
[cell.contentView addSubview:label1];
[标签1释放];
label2=[[UILabel alloc]initWithFrame:CGRectMake(10,25280,10)];
label2.font=[UIFont fontWithName:@“Helvetica”大小:14];
标签2.tag=2;
[cell.contentView addSubview:label2];
[label2释放];
label3=[[UILabel alloc]initWithFrame:CGRectMake(10,40290,11)];
label3.font=[UIFont fontWithName:@“Helvetica”大小:14];
标签3.tag=3;
[cell.contentView addSubview:label3];
[label3释放];
}
其他的
{
//使用前面定义的标记检索标签
label1=(UILabel*)[cell.contentView视图带标记:1];
label2=(UILabel*)[cell.contentView视图带标记:2];
label3=(UILabel*)[cell.contentView视图带标记:3];
}
如果([indexPath行]<[itemsArray计数])
{
NSDictionary*item=[itemsArray objectAtIndex:[indexPath行]];
label1.text=[NSString stringWithFormat:@“Description%@”,[item valueForKey:@“Desc”];
label2.text=[NSString stringWithFormat:@“价格%@”,[item valueForKey:@“价格”];
label3.text=[NSString stringWithFormat:@“Loc%@],[item valueForKey:@“Loc”];
}
返回单元;
}

label1=[cell.contentView view with tag:1];我是否需要将其类型转换为(UILabel*)“不兼容的Objective-C类型分配'struct UIView*',预期为'struct UILabel*”。谢谢Noah:-)感谢您的帮助。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ReuseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *label1;
    UILabel *label2;
    UILabel *label3;
    if( cell == nil )
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,18,280,10)];
        label1.font = [UIFont fontWithName:@"Helvetica" size:14];
        label1.tag = 1;
        [cell.contentView addSubview:label1];
        [label1 release];

        label2 = [[UILabel alloc] initWithFrame:CGRectMake(10,25,280,10)];
        label2.font = [UIFont fontWithName:@"Helvetica" size:14];
        label2.tag = 2;
        [cell.contentView addSubview:label2];
        [label2 release];

        label3 = [[UILabel alloc] initWithFrame:CGRectMake(10,40,290,11)];
        label3.font = [UIFont fontWithName:@"Helvetica" size:14];
        label3.tag = 3;
        [cell.contentView addSubview:label3];
        [label3 release];
    }
    else
    {
        // retrieve the labels using the tags you defined earlier

        label1 = (UILabel *)[cell.contentView viewWithTag:1];
        label2 = (UILabel *)[cell.contentView viewWithTag:2];
        label3 = (UILabel *)[cell.contentView viewWithTag:3];
    }

    if ( [indexPath row] < [itemsArray count] )
    {
        NSDictionary *item = [itemsArray objectAtIndex:[indexPath row]];
        label1.text = [NSString stringWithFormat:@"Description %@", [item valueForKey:@"Desc"]];
        label2.text = [NSString stringWithFormat:@"Price %@", [item valueForKey:@"Price"]];
        label3.text = [NSString stringWithFormat:@"Loc %@", [item valueForKey:@"Loc"]];
    }

    return cell;
}