every body.
I'm a newer man of iphone develop. I hop your help.
I try to add some labels to TabelView.
The code is following.
UITableView *tableView = [[UITableView alloc]init];
for ( int j = 0; j < 3; j++ )
{
UILabel *label = [[UILabel alloc]init];
label.text = [NSString stringWithFormat:#"sample"];
label.frame = CGRectMake (10, 10, 100, 30);
[tableView addSubview:label];
[label release];
}
[tableView release];
But, at the result, i saw some stranges.
It added only in first index of the tableView.
So it is overrided in a index.
What's the problem?
How can i do ?
It is not a init function and i want to add some rows dynamically.
Please help me.
I'm assuming you're populating your tableview correctly (using datasource methods). If you're not, then you need to read the Apple docs urgently (even if your are, you should read them anyway!)
To insert a new row into the table, update the source of your data (say, an array of strings), and then use
-[UITableView insertRowsAtIndexPaths:withRowAnimation:]
this is not the way you are doing it. read these tutorial they will help you
http://mobile.tutsplus.com/tutorials/iphone/uitableview/
http://www.codeproject.com/Articles/79239/iPhone-Programming-Tutorial-UITableView-Hello-Worl
Related
I have a situation here, I want to add user name with a check button in a view, looks simple :)...
Question is I may have single or multiple user, so I need to add it through code only, I want to add all names like a ladder with its corresponding check button.
What is the best way to do that?
I have tried by adding labels for each user.. While adding I wanted to set its top position one by one to make it aligned properly.. Do I have to calculate top like this? Any easiest way is there.. Like how they are doing in Android (relative Layout).
thanks
You can create array of usernames and use following code.
This is test code for your reference.
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:#"1"];
[arr addObject:#"2"];
[arr addObject:#"3"];
[arr addObject:#"4"];
[arr addObject:#"15"];
Now looping:
for (int i = 0; i<[arr count]; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10*10*i, 100, 40)];
label.text = [arr objectAtIndex:i]; //usernames from array
[self.view addSubview:label];
}
The Objective-C equivalent of relativelayout is Cocoa Auto Layout
I would use a UItableView with a custom cell. The cell contains your input fields. This way you can have as many inputs as you like and they will always be layed out in order in the same way, and you don't need to write any funky code. You also get scrolling and memory management for free.
So i have a bunch of dynamically loaded labels..
Each of them has the same name because there is no telling how many there will be..
I have another method (not the one that created the labels) changing the text for one of the labels, but when i run it only the last label that was created will change..
I need it to change the one that has a certain tag or something..
Help is much appreciated, this website is yet to let me down.
self.myLabel cannot be connected to multiple labels, so it will contain the reference of last created label, you will have to create new label every time, and you can't track them by class properties, you have to access label by their tag.
you can set tag for each label, below is sample code,
for(int i=0; i< numberOfLabels; i++)
{
UILabel *label = [[UILabel alloc] init];
label.tag = i; // do not use tag 0 here.. u can use i+1, or i+100.. something like this.
[self.view addSubview:label];
}
to access labels,
UILabel *label = (UILabel*)[self.view viewWithTag: labelTag];
Okay since you dont have any code to show i guess i have to speculate.
What i understood is that you are creating Dynamic UILabels in ur code and you want to access them. Since you have same name for all the UILabels you might me loosing the previous UILabel when every time you create a new UILabel. So in order to keep track of how many UILabel you created you must add them in an Array. Declare an NSMutableArray in your viewController.h file and make sure in the viewDidLoad u allocate it like
arrForLabels = [[NSMutableArray alloc]init];
Since it is an NSMutableArray you can add object to it.
So when u create a UILabel make sure you add the same UILabel in the Array as well
for Instance
[arrForLabels addObject:yourLabel];
you can try to NSLog your Array to see its content.
Now all youu got to do is to Create a weak link like that
UILabel *tempLabel = [arrForLabels objectAtIndex:1];
now tempLabel will be the UILabel to change text
tempLabel.text = #"My New Text";
It will work fine.
Feel free to ask for any issues in it.
I have some confusion. Not really a bug or issue I am facing. I want to know something and I am not sure this is right place for this kind of question but here is my question:
In reference to my Question Here I was trying to map single gesture on two views.
Now I am keen to know that why this is not working? I went on basics that in case I have any NSObject subclass lets say if NSString and I add this in an NSMutableArray as follows:
NSString *strTest = #"Test String";
[aryTest1 addObject:strTest];
[aryTest2 addObject:strTest];
String gets added in both of array. Now what I am doing is I created a label, set tag for the label and added that in two views as follows:
UILabel *lblTemp = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
[lblTemp setTag:1];
[lblTemp setText:#"Label"];
[vw1 addSubview:lblTemp];
[vw2 addSubview:lblTemp];
Now every view has an array of subview. When we try to add an object in that view's subview why this is not being added in both of view?
I am getting a result that my second view is showing the label but first view is not having the label.
If I comment the last line of my code and don't add label in vw2 the label will be added in vw1. Than I tried a different thing.
UILabel *lblTemp = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
[lblTemp setTag:1];
NSMutableArray ary1 = [[NSMutableArray alloc] init];
NSMutableArray ary2 = [[NSMutableArray alloc] init];
[ary1 addObject:lblTemp];
[ary2 addObject:lblTemp];
both array has count 1. Thats mean that I have label added in my array. after that I tried to add array's object in view than even ame problem. Label is only being added in second view:
[vw1 addSubview:[ary1 objectAtIndex:0]];
[vw2 addSubview:[ary2 objectAtIndex:0]];
When I printed retainCount of Label after initialize, adding in array1 and array2 everytime the retaincount was 1. So I think the array are not keeping the reference of same object. Its a new object copied in array. Than why label is not being added in same view?
What is actual internal process for a object to add on any view's subview. As of I know subview is an Array type than what happening? Can anyone explain me?
Thanks in Advance
The difference is that addSubview automatically removes a view from it's super view before adding it to the new view (See the class reference: http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/uiview_class/uiview/uiview.html) while an array will happily accept an object even if it is in another array.
You can create a copy of the label and place it in the other view though if this is what you need to do.
I have the problem in refreshing the table. I create the table like this
for (int i = 0; i < 4; i++)
{
DetailsTable = [[UITableView alloc] initWithFrame:CGRectMake(i*768, 45, 768, 1024) style:UITableViewStyleGrouped];
DetailsTable.dataSource = self;
DetailsTable.delegate = self;
DetailsTable.tag = i + 1;
[scrollView addSubview:DetailsTable];
[DetailsTable release];
}
Whenever I am refreshing the table like this [DetailsTable reloadData]; it refresh the last table only. And other table is not getting refresh
How can refresh all table view in iphone
this is because in DetailsTable the last table reference is retain so that this occurs
Try this
add this in .h file
NSMutableArray *tblArr;
add this in .m file
tblArr=[[NSMutableArray alloc] init];
for (int i = 0; i < 4; i++)
{
UITableView *DetailsTable = [[UITableView alloc] initWithFrame:CGRectMake(i*768, 45, 768, 1024) style:UITableViewStyleGrouped];
DetailsTable.dataSource = self;
DetailsTable.delegate = self;
DetailsTable.tag = i + 1;
[scrollView addSubview:DetailsTable];
[tblArr addObject:DetailsTable];
[DetailsTable release];
}
and when you want to relaod all table use this
for (int i = 0; i < 4; i++)
{
[(UITableView *)[tblArr objectAtIndex:i] reloadData];
}
You keep overwriting the value of the PersonDetailsTable property in the loop. Therefore the value of that property is the 4th one after the loop exits. You call reloadData against that property which points to the 4th instance of the table you added.
To make that work, you would need an NSMutableArray of table views (instead of the one property reference) and reloading all of them would could be looping with reloadData calls. Of course in your UITableView datasource callbacks, you need to distinguish between which table view is calling you back for data since self is the datasource and delegate for all of them.
you are creating 4 tableView not four cells, and every time you are creating a new instance of the table so when you call the [DetailsTable reloadData]; it refrences the most recent instance of DetailsTable i.e the last table.
The variable DetailsTable is being replaced with a new one every time in the for loop.
You should create an array of tables instead, like this:
//Declaration
UITableView *DetailsTable[4];
//Implementation
for (int i = 0; i < 4; i++)
{
DetailsTable[i] = [[UITableView alloc] initWithFrame:CGRectMake(i*768, 45, 768, 1024) style:UITableViewStyleGrouped];
...
}
//And to refresh them
for(int i = 0; i < 4; i++)
[DetailsTable[i] reloadData];
put in for loop where you are going to create
DetailsTable.tag=i
and then for example of edit method of delegate method use this
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (tableView.tag)
{
case 0:
{
//write your code here
break;
}
// like this do all for other remaining tableview
default:
break;
}
}
You can not add tables like this. Your next tableView will replace the pre tableView. You should impliment this by init four single tables.
In my iphone app, I have created some UITextField in scrollView dynamically. And added one button in xib. On that button's TouchUpInside event, I opened UIImagePickercontroller to open photo library and taking selected image on UIImageView.
Now when modalview is dissmissed, values in my UITextField disappears.
How do I retain the UITextField values?
txt = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 30.0f, 200.0f, 30.0f)];
[txt addTarget:self action:#selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit];
txt.textColor = [UIColor blackColor];
txt.borderStyle = UITextBorderStyleBezel;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
[fieldArray addObject:txt];
Using for loop I am adding txt to NSMutableArray fieldArray.
Here is the code where I fetch values from TextFields
NSMutableArray *insertValues = [[NSMutableArray alloc]init];
//Taking values from textFields
for (int i=1; i<=nooffields; i++) {
UITextField *tf = (UITextField *)[self.view viewWithTag:i];
NSLog(#"TF : %#",tf.text);
if (tf.text.length<=0) {
tf.text=#"";
}
[insertValues addObject:[NSString stringWithFormat:#"'%#'",tf.text]];
}
EDIT :
And also when I display values from database in this textFields and try to edit it. It gives me null values.
What may be the reason? If there any other alternatives? please help me
UITextField *textField=[[UITextField alloc]init];
[textField setFrame:CGRectMake(x,y, width, hight)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:textField];
The information that your scrollview uses to get populated should be stored outside the scrollview, in a NSMutableDictionary for example. So everytime cellForRowAtIndexPath gets called it can retrieve the information from that source. So I advise you to store the UITextField's text within the dictionary. You can index it by its tag number. That way you won't loose whatever it contained.
I hope it helps you
Cheers
I finally got a work around for my problem.
I am temporarily storing my textField's values into database when presentModalViewController is called.
These values I retrieve back into the textFields on viewWillAppear.
This may not be a very efficient solution but it worked in my case.
Hope this helps someone.
Thanks for all your responses.