UITextView显示数组中的所有值

时间:2013-09-03 07:10:59

标签: iphone nsmutablearray nsarray uitextview

我在服务器中存储了一些文本值。我成功获取了该文本值并存储到本地数据库文件中。然后将该值添加到数组中。当我将数组值显示给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 

2 个答案:

答案 0 :(得分: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;

}

答案 1 :(得分:0)

这是因为For循环。你只需要提取值而不是在这里调用For循环,例如:

txt.text = [descript_array objectAtIndex:i];

您需要的只是区分Button Action调用,即: - 按下了哪个按钮以及从Array中提取的元素。因此,要区分呼叫,您只需将Tag设置为实现Gesture的映像。标签将帮助您区分手势调用,例如:

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

在Gesture委托方法中:

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