2

I have to display NSmutableArray data in UIlabel .But only the last index text is getting added .Here is my code

marray:
(
{ 
ID=1;
}
{
ID="2"
}
)

 for(int i=0;i<[marray count];i++)
    {
        eventName=[[arr2 objectAtIndex:i]objectForKey:@"Code"];
       label.text=[NSString stringWithFormat:@"ID :%@",eventName ];        
    } 

I have to display these ID's in UIlabel with text as : 1 , 2

How can I do it?

5 Answers 5

1

you can append string to NSString object than use that object in label like bellow

NSMutableString *lblstr=@"ID :";

for (int i=0; i<[marray count]; i++) {
    eventName=[[arr2 objectAtIndex:i]objectForKey:@"Code"];
    [lblstr appendString:[NSString stringWithFormat:@"%@",eventName ]];
}
label.text=lblstr;
Sign up to request clarification or add additional context in comments.

2 Comments

Appending the string wont help it being displayed coz the for loop runs pretty fast doesn't it ?
I m getting error at this line [lblstr appendString:[NSString stringWithFormat:@"%@",eventName ]];.Program received SIGABRT ..IN console area I can see "Attempt to mutate immutable object with appendString:'"What shall I do?
1

You are running a for loop which will continue executing till its last increment . So the last value in the array would be displayed. So what you can do is that you can add a timer instead of a for loop that is if you want to show your NSmutableArray as changing in the UILabel.

EDIT:

-(IBAction) rotate3
{
    NSString *number = [self.dayArray description];
    NSArray *array = [[NSArray alloc] initWithObjects: @"0", @"1", @"2", @"3", @"4", @"5" ,@"6", @"7", @"8",@"9",@"10",@"11",@"12",@"13", @"14", @"15", @"16", @"17", @"18", @"19",nil];
    numberCount++;
    timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(rotate3 )userInfo:nil repeats:YES];    
    self.dayArray = array;
    [array release];

    label.text = [NSString stringWithFormat:@"Day %@  ", number];
}

Also check these links How to update UILabel programmatically in iOS and How to display an NSArray in a UILabel and use timer

1 Comment

But I want all the items of NSMutableArray in label's text>How can I do it?
0
Try the below Code. 
NSString *temp=@"";
for(int i=0;i<[marray count];i++)
    {

        //eventName=[[arr2 objectAtIndex:i]objectForKey:@"Code"];
        temp=[temp stringByAppendingString:[[arr2 objectAtIndex:i]objectForKey:@"Code"]];

        temp=[temp stringByAppendingString:@" ,"];
    } 
 temp = [temp substringToIndex:[temp length]-1];
label.text=[NSString stringWithFormat:@"ID :%@",temp ];

7 Comments

.I got it working partially .I want in this format 1 , 2.i.e a space and comma after each item
Check my edited Answer if it is useful plz accept it my clicking on checkmark and upvote by up arrow
Thanks I got it. I have a small question .Here if I have more Ids like 3,4,5,6 ...then am unable to see it in label .that part is cut .so if I want to display content in such a away that if its not sufficient in the first line then it should be displayed in the next line and so on....How can I do it ?this is the label I m doing label=[[UILabel alloc]init]; [label setFrame:CGRectMake(20,175,330, 60)]; label.textAlignment=UITextAlignmentLeft; label.backgroundColor=[UIColor clearColor];
You can make multiLine label. yourLable.numberofLines=10; then increase the height of label. or you can use UITextView for scrolling Label does not scroll you need to increase the height according to your data.
And you can use the description method of NSSArray to get all contents in string by NSString *arrContents=[arr description];
|
0
NSMutableString *str = [[NSMutableString alloc] init];

for(int i=0;i<[marray count];i++)
{
   NSString *temp = [NSString stringWithFormat:@"%d - %@\n",i+1, [[arr2 objectAtIndex:i]objectForKey:@"Code"]];

  [str appendString:[NSString stringWithFormat:@"%@",temp]];

   //sleep(0.05); //Remove comment this line may help ! **Tricky Line**
}

label.text = str;

[str release];

Comments

0

You may create an array of objects (indexes) and use componentsJoinedByString:

NSString* str = [array componentsJoindeByString: @", "];

Or using your code, replacing

label.text=[NSString stringWithFormat:@"ID :%@",eventName ]; 

with:

label.text=[label.text stringByAppendingFormat:@", %@", eventName]; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.