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.
Related
Hi I have a table view with static cells. I want to hide specific ones with a button. I must hide them according to the current time of the iphone so i created UITableViewCell outlets for each of them. How can i hide a cell when the button is clicked ? Thanks in advance.
I get what you want to do. if you want to hide action, you need to control your static tableView.
Don forget to reload tableview in your action.
[self.tableView reloadData];
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
cell.hidden=YES;
return 0;
}else{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
cell.hidden=NO;
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
return 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!
I'd like to do following: After clicking a row in a tableview, I want to set value of some variable and in the same time change the cell accessory type to checkmark. Here is my implementation file:
- (void)viewDidLoad
{
[super viewDidLoad];
tabledata = [[NSArray alloc] initWithObjects:#"row1", #"row2", #"row3", nil];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - TableView Data Source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tabledata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"Row"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Row"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
return cell;
}
#pragma mark TableVoew Delegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//if row1 is selected change 'value' to 1 and change cell accessory to checkmark (other cells accessory to none)
//if row2 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
//if row3 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.value = indexPath.row;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
EDIT
Set only the accessory for selected row in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell.accessoryType = UITableViewAccessoryNone;
if (indexPath.row == self.value) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
See this to remove checkmarks from currently visible cells in tableview.
EDIT 2
To unselect the previous row, do this in didSelectRowAtIndexPath
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:index inSection:1];
NSLog(#"Oldindexpath is: %#", oldIndexPath);
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
if (oldCell != newCell){
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
}
This solved my problem with removing old checkmarks:
for (UITableViewCell *cell in [tableView visibleCells]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
and problem with setting value of current row was solved in previous answer.
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;
}
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).