xcode uitableview - xcode

i have an array of objects which i am populating in a UItable one of my attributes of each object is a YES/NO value
Im using the- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { to populate my table as follows .
My code creates a table entry for each object in the array. I would like to only populate the table using objects in the array which have the "display" attribute set to YES? how do i do this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellidentifier = #"customcell";
customcell *cell = (customcell *)[tableView dequeueReusableCellWithIdentifier:
cellidentifier];
my_details *myObj = [appDelegate.myArray objectAtIndex:indexPath.row];
// UITableViewCell cell needs creating for this UITableView row.
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"customcell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[customcell class]]) {
cell = (customcell *) currentObject;
break;
}
}
}
cell.line1.text = myObj.line1;
cell.line2.text = myObj.line2;
cell.line3.text = myObj.line3;
return cell;
}

It would be a good idea to try to filter the objects before sending them to the table for display.
You could use an NSPredicate to filter the array of objects and when an object changes it's state, the display attribute is set to yes, refilter the array and pass it to the tableview.
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"display == YES"];
NSArray *filteredArray = [myObjectsArray filteredArrayUsingPredicate:testForTrue];
after you obtain the filteredArray you need to pass it to the tableView and tell it to reload it's data in order to refresh.

As #Sorin indicates you have to filter your table. This can be done before or during the insertion in the table. Which one is a matter of taste (and which guidelines you normally follow for programming)
a) Before
Have all data -> filter -> Have reduced set -> Display reduced table
b) During
Have all data -> count #items -> introduce in rows -> Display table
For that you have to adapt numberOfRowsInSection (pseudo code!)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
counter=0;
for each in table
if value = YES counter++
return counter;
}
In the cellForRowAtIndexPath you now have to keep track of the next line to insert, via a global variable: (pseudo code!)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
rowcounter++;
cell.textLabel.text=[selectionTableArray objectAtIndex:rowcounter];
return cell;
}

Related

populate table view with prototype cells

I have this scene:
I added one simple "table view" and next +1 into "prototype cells":
informacao.h
NSMutableArray *arrCursos;
informacao.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Informações";
arrCursos = [[NSMutableArray alloc] init];
[arrCursos addObject:#"[G] - Odontologia"];
[arrCursos addObject:#"[P/LT] - Odontologia"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrCursos count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cursosCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [arrCursos objectAtIndex:indexPath.row];
return cell;
}
I don't know why my table view is empty, like this:
Now, there are a few places you should check. First is whether the table view data source has been connected to the view controller. Check if the number of rows data source method is called and whether it is returning the expected number of rows.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"%d", arrCursos.count);
return [arrCursos count];
}
Second is to check whether you have specified the cell identifier cursosCell in your storyboard.
the problem was missing delegate and datasource

How to change subview's title from a table selected row in Xcode?

Here is the problem: I have a table view, custom cells, and detail view. I have 10 rows in my table and when the user selects one of the rows, the detail view opens. How to set the detail view nav bar's title from the selected row of my table view? I have three classes - televisionList (my tableview), televisionCell (my cells in the table view) and televisionDetail (my detail view which opens when a row is selected). In my televisionCell I have declared the following label:
#property (weak, nonatomic) IBOutlet UILabel *tvLabel;
In my televisionList (the tableview) I have this method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
televisionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"televisionCell" owner:self options:nil];
for (id currentObject in objects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (televisionCell *)currentObject;
break;
}
}
}
switch (indexPath.row)
{
case 0:
{
cell.imageView.image = bnt;
cell.tvLabel.text = #"БНТ 1";
cell.backgroundView.image = [UIImage imageNamed:#"lightbackgr.png"];
break;
}
case 1:
{
cell.imageView.image = btv;
cell.tvLabel.text = #"bTV";
cell.backgroundView.image = [UIImage imageNamed:#"background-cell.png"];
break;
}
case 2:
{
cell.imageView.image = novatv;
cell.tvLabel.text = #"Нова TV";
cell.backgroundView.image = [UIImage imageNamed:#"lightbackgr.png"];
break;
}
}
Now, I want to change my detail view title to be equal to cell.tvLabel.text when a row is selected. Where and how to do that? Thanks in advance!
As you wrote, you want to set the title, when the cell is selected, so the best way will be to use:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
televisionCell *cell=[tableView cellForRowAtIndexPath:indexPath];
details.title=cell.tvLabel.text;
}
However, you may consider introducing the model object to your app and keep them in some array(objects in the example below`. So the method would look like:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YourObject* obj=[self.objects objectAtIndex:indexPath.row];
details.title=obj.stationName;
}

Search Result in TableView Not Using Custom Cell

i have added a search bar to my table view and it is working and returning results correctly. However, the results are showing up, using my custom cell, but the underlying table does not "stretch" to fit the cell. I hope that makes sense. Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomBuyMainCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
Item *item = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
item = (Item *)[self.filteredItemArray objectAtIndex:indexPath.row];}
else {
item = (Item *)[self.itemArray objectAtIndex:indexPath.row];}
I believe it has something to do with this code. But i'm not sure. It seems the searchResultsTableView does not know about the custom cell...and the custom cell just overlays on top of it and the underlying table doesn't accommodate it. Any help is greatly appreciated!

Xcode bug? VoiceOver with TableView

I was just wondering is this an existing bug OR is there anyway to fix this. When in tableview, turning on voiceover will result in empty table cells... anyone?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"tableCell";
//Exisint bug if using TableViewCell
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.pLabel.text = [self.tableArrayPlaces objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedDest = [tableArrayPlaces objectAtIndex:indexPath.row];
//Get the selected country
NSLog(#"SelectedDest %#", selectedDest);
}
Edited: Codes are added. This is my codes for populating my cells. In storyboard I set it to "push", can't seem to work. Any help?
It might be a bug in iOS or in your code, but it's probably not a bug in Xcode.
Check your table view controller's -tableView:cellForRowAtIndexPath: method. Is it being called when the empty cells are generated? If no, why not? If yes, is it returning properly formatted cells? Can you reproduce the problem in other applications such as Contacts?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"tableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.text = [tableArrayPlaces objectAtIndex:[indexPath row]];
return cell;
}
Instead of using my own cellView, I used UITableViewCell. This solves my problem. But of course I can no longer do any linking at my storyboard directly.

<NSInternalInconsistencyException> Invalid update: invalid number of sections

I am adding user defined text in a UItableView via a modal view controller.
The table is created as a NSMutableArray. on entering data in the modal view and tapping a Done button a Invalid update: invalid number of sections. error is generated.
I must update the array when the user enters data but not sure how to do that. I have attached the relevant code. Thanks for your advice.
//Number of sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
//Number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listOfConjProcedures count];
}
//Loading the 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];
}
// Configure the cell.
cell.textLabel.text = [listOfConjProcedures objectAtIndex:indexPath.row];
return cell;
}
//Done button Delegate for the modal view
- (void)itemDetailViewController:(ItemDetailViewController *)controller didFinishAddingItem:(ChecklistItem *)item
{
int newRowIndex = [listOfConjProcedures indexOfObject:item];
[listOfConjProcedures addObject:item];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
}
I think your problem may be here:
int newRowIndex = [listOfConjProcedures indexOfObject:item];
This wouldn't have an index unless you'd already added it to the array, but in the next line you add it to the array, so it doesn't make sense.
I think you are therefore adding a row at index path 0,NSNotFound, which will confuse the table view. Either use the count of the array as the row index, or insert your new row at the start (0,0) of the table (and at index 0 of the array).

Resources