【问题标题】:UITextView displaying all the values from arrayUITextView 显示数组中的所有值
【发布时间】:2013-09-03 07:10:59
【问题描述】:

我在服务器中存储了一些文本值。我成功地获取了该文本值并存储到本地数据库文件中。然后将该值添加到数组中。当我将该数组值显示到 UITextView 时。但是 textview 显示所有值。我有点击索引的按钮。如果我单击第一个按钮,我需要显示第一个文本。然后下一个按钮。如果我单击每个按钮,它会显示所有值。

代码:

-(void)dbClicked:(id)sender{


    [mmageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[productimg_array objectAtIndex:[sender tag]-1]]]]];


    [self.view addSubview:mmageView];
UITapGestureRecognizer *singletapp=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    singletapp.numberOfTapsRequired=1;

    [mmageView addGestureRecognizer:singletapp];

}



- (void)singleTap:(UIGestureRecognizer *)singletap {

        UITextView *txt=[[UITextView alloc]initWithFrame:CGRectMake(50, 50, 150,150)];
                NSMutableString *string = [NSMutableString string];    

            for(int i=0;i<[descript_array count];i++){

                NSLog(@"descript array is  is %d",i);

      txt.text = [descript_array objectAtIndex:i];


              }

            [self.view addSubview:txt];

}

NSLog:

descript array is  is 14

string is the name is  image with black color
the name is image with black color
the name is image with white color
the name is 
the name is 
the name is 
the name is 
the name is 
the name is 
the name is 
the name is 
the name is 

descript array is  is 15

    string is the name is  image with black color
    the name is image with black color
    the name is image with white color
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 

【问题讨论】:

    标签: iphone nsmutablearray nsarray uitextview


    【解决方案1】:
    - (void)viewDidLoad
    {
        UITextView *txt=[[UITextView alloc]initWithFrame:CGRectMake(50, 50, 150,150)];
        [self.view addSubview:txt];
    
        NSString *str_text;
    
        for(int i=0;i<[descript_array count];i++)
        {
            str_text = [NSString stringWithFormat:@"%@ %@",str_text,[descript_array objectAtIndex:i]];
            //[str_text retain];
        }
        txt.text = str_text;
        txt.editable=FALSE;
    
    }
    

    【讨论】:

    • wahooo...您将 str_text 多次保留为编号。数组中的元素!对于 1K 条目,将有 1K 保留!
    • 然后 str 保留在循环中
    • @BhaveshNai 无需重新启动 str。是的,它保留在循环中。如果您正在重新启动,请设法释放。从上面的代码看来,浪费了很多内存。
    • 我得到str是(null)黑色图像名称是黑色图像名称是白色图像描述是(黑色图像名称是黑色图像名称是白色图像“”、“”、“”、“”、“”、“”、“”、“”、“”、
    • @user2674668 make str alloc init.,,
    【解决方案2】:

    这是因为 For 循环。您只需在此处提取值而不是调用For 循环,例如:

    txt.text = [descript_array objectAtIndex:i];
    

    您只需要区分按钮操作调用,即:- 按下了哪个按钮以及从数组中提取哪个元素。因此,为了区分调用,您只需将一个标签设置为实现手势的图像。该标签将帮助您区分手势调用,例如:

     [mmageView setTag: 10]; //Let say 10
    

    在手势委托方法中:

    - (void)singleTap:(UIGestureRecognizer *)sender {
    
           if (sender.tag == 10) 
           {
                 UITextView * yourTextView=[[[UITextView alloc]initWithFrame:CGRectMake(50, 50, 150,150)] autorelease];
    
                 yourTextView.text = [descript_array objectAtIndex:10];  //give appropriate index to extract value
    
           }
                [self.view addSubview:txt];
    
    }//End of method
    

    【讨论】:

    • @user2674668:基本上你已经使用循环来显示所有的值。当然,在 For 循环中,每个值都会显示出来!如果您只需要显示一个值,那么您需要像我回答的那样提取值!
    • @user2674668:你说“我有按钮点击索引。如果我点击第一个按钮,我需要显示第一个文本。然后下一个按钮。如果我点击每个按钮,它会显示所有值。” ..您可以编辑您的问题并将 IBActions 放入您的代码中,以便我可以进一步指导您吗?
    • @user2674668:是的,我见过。您需要将按钮单击(IBAction)代码。我的意思是您如何执行按钮操作?写那个代码
    • 如果我使用 txt.text = [descript_array objectAtIndex:i]; textview 仅显示数组中的最后一个值..
    • @user2674668:屏幕上有多少个按钮?您可以发布您要实现的目标的示例图片吗?因为我仍然不清楚你为什么使用手势而不是实现简单的 IBActions 来点击按钮?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2012-10-25
    • 2014-11-17
    • 1970-01-01
    相关资源
    最近更新 更多