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.
Related
I have one NSMutableArray that has number of records (suppose 52).
for (int i=0;i<=52;i++) {
[arrSavedCalculation addObject:[NSString stringWithFormat:#"Bianca lb,Red %d",i]];
}
I want to give a paging Prev and Next button at bottom of tableview. and each page display 6 record.How can i do this. i also see this document but not success
http://www.ke-cai.net/2011/04/pagination-with-uitableview-in-ios.html
Try with these. Tested and found working. Can be improved.
#interface ViewController (){
//pageNumber will hold the current page index
NSInteger _pageNumber;
//keeping the max page number for ease of calculation
NSUInteger _maxPageNumber;
//the batch size
NSUInteger _numberOfVisibleRows;
}
#end
#implementation ViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Main data array is formed
NSUInteger rows = 0;
NSMutableArray *tempArray = [NSMutableArray array];
while (rows<52) {
[tempArray addObject:[NSString stringWithFormat:#"Item %d",rows+1]];
rows++;
}
self.mainArray = [NSArray arrayWithArray:tempArray];
_pageNumber = 0;
_numberOfVisibleRows = 5;
_maxPageNumber = [self.mainArray count]/_numberOfVisibleRows;
self.subArray = [self subArrayForPageNumber:_pageNumber];
}
- (NSArray *)subArrayForPageNumber:(NSUInteger)pageNumber{
NSRange range = NSMakeRange(_pageNumber*_numberOfVisibleRows, _numberOfVisibleRows);
if (range.location+range.length>[self.mainArray count]) {
range.length = [self.mainArray count]-range.location;
}
return [self.mainArray subarrayWithRange:range];
}
- (IBAction)buttonPressed:(UIBarButtonItem *)button{
//Same method is used for calculating the page numbers
if (button.tag ==1) {
_pageNumber= MIN(_maxPageNumber, _pageNumber+1);
}else{
_pageNumber = MAX(0, _pageNumber-1);
}
self.subArray = [self subArrayForPageNumber:_pageNumber];
[self.tableView reloadData];
}
Source Code
you should take two array.
1) Your MAIN ARRAY containing all of objects.
2) A Temporary array contain only 6 objects.
While loading tableview use that Temporary Array.
keep a page counter which will count your current page.
According to your 52 objects you can have 52/6 = 9 page but last page contain only 4 object.
like.
#define kNumberOfObjectInOnePage 6
set self.page=0 in "viewDidLoad" //self.page is the page counter.
-(void)nextPage:(id)sender{
self.page++;
take 6 objects from main array into temp array
reload your table view.
}
-(void)previous:(id)sender{
self.page--;
take 6 previous object into temp array
reload your table view.
}
A simple solution is if you are accessing records from web service then ask to him provide the total number of records in initial hits as well as first 6 records to display. If you want to do paging you should keep page 0 or 1 at initial hit to service and as you got 6 records on first page and you find that total records which are given from service is more than the 6 then you have to show previous and next button otherwise no need of these button.
As you accessing the records you have to put into in an NSArray or in Coredata. When you press next buttonyou have to increase the page count and fetch next 6 records and save to DB or array and reload the table.
if you press previous button you have to remove last 6 object from array or you can delete or check some kind last 6 records to display and reload table.
Hope this helps.
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
I have this table view which has to add button.
this add button opens a view where the user can enter something in a textfield.
when the user is done he pressed a done button which put the text in the textfield in to 2 public variables then it spawn the tableview again but when the table view is spawned again it has the entered text as a object i need this process so it can be done unlimited times. but i don't know who i can make this please help
i have been traying to make this code by my self and this is what i got to
this is the tableview
-(void)viewWillAppear:(BOOL)animated{
if(appdel.sExerciseName != NULL){
NSString *newitem = appdel.sExerciseName;
appdel.newExerciseArray = [[NSMutableArray alloc]initWithObjects:newitem, nil];
NSLog(#" number of objects in array is %i",[appdel.newExerciseArray count]);
int x = 0;
do{
NSLog(#"23 pos is : %i and object is %#",x,[appdel.newExerciseArray objectAtIndex:x]);
for (int y =0; y<[appdel.newExerciseArray count]; y++) {
[exercises addObject:[appdel.newExerciseArray objectAtIndex:x]];
}
x++;
}while (x<[appdel.newExerciseArray count]);
appdel.sExerciseName = NULL;
[Table reloadData];
} }
this is the donebutton
-(IBAction)DonPressed{
appdel.sExerciseName = NameField.text;
appdel.sExerciseTimes = timesField.text;
NSLog(#"name %# and times is %# button pressed1",appdel.sExerciseName , appdel.sExerciseTimes);
exercisesViewControler *detailViewController = [[exercisesViewControler alloc] initWithNibName:#"exercisesViewcontroler" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
so this code works first time but when i due it second time it just overwrite the old object / item in the array with contains the data off the table view
sry for my english and please keep in mind when you answer keep it simple so i can understand as a noob developer
the problem is, that you are creating new instances of exercisesViewController when you are done, by calling:
[[exercisesViewControler alloc] initWithNibName:#"exercisesViewcontroler" bundle:nil];
But what yout want to do is to keep the object. Instead you could do the following:
[self.navigationController popViewControllerAnimated:YES];
So your new method will look like:
-(IBAction)DonPressed{
appdel.sExerciseName = NameField.text;
appdel.sExerciseTimes = timesField.text;
NSLog(#"name %# and times is %# button pressed1",appdel.sExerciseName , appdel.sExerciseTimes);
[self.navigationController popViewControllerAnimated:YES];
}
I have a program that I want to either hide or show certain UIbuttons depending on certain variables and all the buttons are named incrementally like 'button1, button2, button3.'
So I want to iterate through the buttons but I don't know how to address the button in an assignment statement using an nsstring as a variable inside a dot notation assignment, such as:
for (int i = 1; i < weekday; i++) {
int buttonIncrement = 0;
NSString *tempString = [[NSString alloc] initWithFormat:
#"calbutton%i", buttonIncrement];
self.tempString.hidden = YES;
}
The "tempString" part of the assignment I want to tell it to insert "calbuttonx" where x is the button number.
Can you do this somehow? if so please explain.
Use an array!
If you can't use an array, you can reference to a property by string with Key-Value Coding (KVC):
UIButton* button = [self valueForKey:tempString];
button.hidden = YES;
You can also assign a tag to each button in IB and get the button associated with the tag using
- (UIView *)viewWithTag:(NSInteger)tag
as defined on class UIView, for example:
for( int k = 0; k < 5; ++k ) {
id subview = [self.view viewWithTag: k];
if( subview ) {
...
}
}
Ok I have 8 labels and I want to loop through them but am having no luck.
This is what I have tried.
for (int i; i = 0; i < 10; i++)
{
double va = [varible1.text doubleValue] + i;
int j = 0 + I
label(j).text= [[NSString alloc]initWithFormat:#"%2.1f", va];
}
This errors out. My labels are named like this label0, label1, label2
Any help would be appreciated.
label(j) is NOT equivalent to label0, label1, etc.
You should create an NSArray of labels, then you can access them with [arrayOfLabels objectAtIndex:j]. If you're not sure what this means, please read the documentation about NSArray...
You should maybe add all your labels to a C array, probably in -viewDidLoad
UILabel* labels[] = { label0, label1, label2, ... };
(not entirely sure about the syntax)
and then access them like
labels[i].text = ...
By the way, I think you're leaking memory here:
labels[i].text = [[NSString alloc]initWithFormat:#"%2.1f", va];
initWithFormat: will return a string with a retain count of 1. labels[i].text will retain that value again. You should release the string after setting the label's text. I'd probably just autorelease it here:
labels[i].text = [[[NSString alloc]initWithFormat:#"%2.1f", va] autorelease];
or use stringWithFormat (which returns an autoreleased string):
labels[i].text = [NSString stringWithFormat:#"%2.1f", va];
for (UILabel *lbl in self.view.subviews)
{
[lbl setFont:[UIFont fontWithName:#"AppleGothic" size:22]];
}
it will change all the labels in your ViewController by just giving tags to labels.
If you cannot or do not want to put your labels in an array, you could iterate through the UIViews using the tag field as an index. You store the index numbers in them (either through IB or programatically) and then get each label using: (UIView *)viewWithTag:(NSInteger)tag.
See below (set theView to the view your labels reside in):
for (int i; i = 0; i < 10; i++)
{
double va = [varible1.text doubleValue] + i;
UILabel * label = [theView viewWithTag: i];
label.text= [[NSString alloc]initWithFormat:#"%2.1f", va];
}