i need to pass array of class objects
i have class called Student , it has some properties (name , code , account_type ....);
i call method in database to get all students object
Student_array = [data_base_helper selectAllStudents];
i need to show list of student names in table view, i made anther array called names
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Student_array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cell_identifer = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identifer forIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_identifer];
}
for(Student * st in student_array){
[self.names addObject:st.student_name];
}
cell.textLabel.text = [names objectAtIndex:indexPath.row ];
return cell;
}
i find some problem with delete as i delete by index idea how to pass array of objects in table view
The idea is that you don't need to use an "array of names", you can just use your Students array directly, in this way:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Student_array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cell_identifer = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identifer forIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_identifer];
}
Student *student = Student_array[indexPath.row];
cell.textLabel.text = student.student_name;
return cell;
}
When you'll need to delete a student from the tableView, you can easily remove from the array and then update the tableView accordingly, something like that:
[Student_array removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
Note that in the code above I'm assuming that you want to delete the object at the given indexPath.row and that Student_array is a NSMutableArray and not a NSArray
ps: you may want to rename Student_array in students since in objective-c it's not a good practice to name objects with a capital letter, nor to use underscores. Likely I would also rename student_name in name since student is just a repetition of the type of the object.
When You Deleted a cell from table delete items for both array Student_array and self.name array corresponding the deleted cell.
Related
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *cellIdentifier = #"Cell";
customcell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[customcell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.finacialyear.text=[self->entries1 objectAtIndex:indexPath.row];
cell.term.text=[self->entries2 objectAtIndex:indexPath.row];
cell.balance.text=[self->entries3 objectAtIndex:indexPath.row];
cell.tintColor = [UIColor blueColor];
return cell;
}
have to add the rows values in the tableview cell and display in to the final row.can anyone suggest me how to do this in objective c.
thanks
First you need to add one extra row:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return <#rows you have now#> + 1;
}
Then you should return a different cell if you are in the last row:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isLastRow) {
return BalanceSumCell;
} else {
return customcell;
}
}
Remember to name your classes with a capital first letter (CustomCell and not customcell).
If you want to do different calculations when the user selects a cell, you should do that in -didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Keep track of user's selection and update the last cell as needed
}
In general, your question is broad and if you break it down to smaller problems you will find a lot of related answers to help you.
Good luck!
I'm trying to add a data source inside a UITableView. I tried the following, but unfortunately it didn't work:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 8;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Set the data for this cell:
cell.textLabel.text = [_classCellview objectAtIndex:indexPath.row];
cell.detailTextLabel.text = #"One";
cell.detailTextLabel.text = #"Two";
// set the accessory view:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
cell.textLabel.text = [_classCellview objectAtIndex:indexPath.row];
You're telling the table that there are 8 sections, but you're not taking the section from the index path into account when you populate the cell. An index path has (in the case of a table view) two values: a section and a row. You need them both to know what cell you're dealing with. So, you're probably getting the same content repeated in each section of your table. (It'd help if you told us exactly what the problem is, though.)
Sorry if this has been discussed already, I couldn't find what I was after..
I have a .plist file which has 2 arrays in it, which are split exactly how I would like my sections split in my table view.
I have no problem getting an array into a table view, but I cant get my head around how to tell the app that I want one array in the first section and the second array in the second section.
My code at the moment to get an array into a table is this (and it works fine):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Create string with reference to the prototype cell created in storyboard
static NSString *CellIdentifier = #"PlayerNameCell";
//Create table view cell using the correct cell type
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Create a dictionary containing the player names
NSDictionary *players = (NSDictionary*) [[self Squad] objectAtIndex:[indexPath row]];
//Set the cell text to the player name
[[cell textLabel] setText:(NSString*)[players valueForKey:#"PlayerFullName"]];
//Set the cell detail text to the squad number
[[cell detailTextLabel] setText:(NSString*)[players valueForKey:#"PlayerSquadNumber"]];
return cell;
}
But now I have another table view where I'll need 2 sections, each reading from different arrays.
Any help would be greatly appreciated.
Many thanks
Just do the following:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"PlayerNameCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if( cell == nil ){
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if( indexPath.section == 0 ){
NSDictionary *players = (NSDictionary*) [self.array1 objectAtIndex:indexPath.row];
cell.textLabel.text = (NSString*)[players valueForKey:#"PlayerFullName"];
cell.detailTextLabel.text = (NSString*)[players valueForKey:#"PlayerSquadNumber"];
} else {
NSDictionary *players = (NSDictionary*) [self.array2 objectAtIndex:indexPath.row];
cell.textLabel.text = (NSString*)[players valueForKey:#"PlayerFullName"];
cell.detailTextLabel.text = (NSString*)[players valueForKey:#"PlayerSquadNumber"];
}
return cell;
}
Set the number of sections to 2. For each section get the different values with [self.array2 objectAtIndex:indexPath.row].
I don't know how you are saving the plist into 2 arrays, but if you need help with that let me know.
Okay, so you have 2 arrays at the top level, and each of those arrays contains arrays, right?
You've got a couple methods you need to adjust. Return the number of top level arrays for the number of sections in the table view. In cellForRowAtIndexPath:, return the correct object for the correct section/row. Something like
[[sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
i m quite stucking with this problem. i have a NSArray *list holding 70 elements from a plist file. now i need to populate them to UITableView.
what i want is to display 20 items at a time, then click the 21st cell to display another 20.. then press the 41st cell to display another 20 items... and so on till all 72 cells reveal..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
static NSString *addCellID = #"addMoreCell";
UITableViewCell *addCell = [tableView dequeueReusableCellWithIdentifier:addCellID];
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"labelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
} else if (indexPath.row < 25){
NSDictionary *dic = [self.list objectAtIndex:indexPath.row];
cell.textLabel.text = [dic objectForKey:#"Title"];
cell.detailTextLabel.text = [dic objectForKey:#"Tips"];
return cell;
} else {
if (cell == nil) {
addCell.textLabel.text = #"Load More...";
return cell;
}
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 20)
{
[self.tableView reloadData];
}
}
Take an array, in which you will add 20 elements from plist, and
set numberOfRowsInSection = [array count] and
check if numberOfRowsInSection < totalNumberOfRecords in plist, if it is less, then increment numberOfRowsInSection by 1.
And in cellForRowAtIndexPath
check if (indexPath.row < totalNumberOfRecords -2 && indexPath.row < numberOfRowsInSection-1)
then add "LoadMore Cell" else not.
So I do something quite similar. You have an array with 70 strings (or dictionaries) to show in the table. The 'numberOfRowsInSection:' should be using an ivar which is initially set to 20.
When your button gets pressed, you increase the ivar by 20 (but to no more than the actual amount of data!), you reload the table, then you can scroll the table up to the "new" cell using tableView methods.
in the method below why indexPath.Row always return 0???
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[cell.textLabel setText:[mainItems objectAtIndex:indexPath.row]];
return cell;
}
mainItems is a NSMutableArray and has 5 items ("item 1", "item 2",...) the View does get 5 items... but they are all "Item 1" and NOT item 1, item 2...
what am i doing wrong?
My guess is that you're returning the wrong value in your implementation of -numberOfSectionsInTableView, and the table view is asking you for the 0th row of 5 sections.
Table views have sections, and each section has rows. Many table views only have one section, but many rows in that section. It sounds like you have one section, with 5 rows. Be sure that you return the correct values in each of:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section