TableView Cells not showing up after search - ios

After searching, I have an array with new data. I have confirmed this with print debugging
Then when it runs cellForRowAtIndexRow, my custom cell doesn't init. So I added an if statement to check if my cell is nil, and if it is reinitialize it. However, when the cell is returned, it doesn't update on my view controller.
When I print cell.cellLabel.text after my search, it is nil. This is most likely the reason why it isn't updating, even though I have confirmed that there is data in direntry
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UIImage *myImage = [UIImage imageNamed:#"Demo"];
static NSString *CellIdentifier = #"peopleCell";
PeopleCell *cell = (PeopleCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = (PeopleCell *)[[PeopleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
DirEntryItem *direntry;
if(tableView == self.searchDisplayController.searchResultsTableView){
direntry = [self.searchResults objectAtIndex:indexPath.row];
}else{
direntry = [self.peopleArray objectAtIndex:indexPath.row];
}
cell.name = direntry.fullname;
cell.phoneNum = direntry.phoneNum;
cell.email = direntry.email;
cell.admin = direntry.admin;
cell.cellLabel.text = direntry.fullname;
[cell.profile setImage:myImage];
return cell;
}

Related

How to change a UITableViewCell image when the cell is selected?

The question sums it up-- I'm trying to have a box get checked when someone clicks on the UITableViewCell, which involves changing the image in the cell. The table is set up well and works just fine-- it's displaying the information that I want it to, and the cells select like they should. However I can't get the image to change when the method didSelectRowAtIndexPath is called.
Here's what I have so far. I've removed extraneous code (from other tables, etc), but this should be everything that's relevant.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == diabetesVisitTable) {
if (indexPath.section==0) {
cell.textLabel.text = [_microvascularValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Family Med Unchecked Box.jpg"];
}
else if (indexPath.section==1) {
cell.textLabel.text = [_macrovascularValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Family Med Unchecked Box.jpg"];
}
else if (indexPath.section==2) {
cell.textLabel.text = [_bloodSugarValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Family Med Unchecked Box.jpg"];
}
cell.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];
self.diabetesVisitTable.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];
return cell;
}
else return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.imageView.image = [UIImage imageNamed:#"Family Med Checked Box.jpg"];
}
In your tableView:didSelectRowAtIndexPath: implementation, you're asking the table view for a new cell to configure with dequeueReusableCellWithIdentifier:. You want to fetch the actual cell used for that indexPath so you can update it directly:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"Family Med Checked Box.jpg"];
}
You'll also need to update the data source for the table view somehow to record the fact that one of the rows is selected, and update tableView:cellForRowAtIndexPath: to set the checked image if the data source indicates that the row is selected. If you don't do those steps, the cell will appear unselected when it refreshes.
UITableViewDelegate has 3 methods to handle cell highlighting:
- tableView:shouldHighlightRowAtIndexPath:
- tableView:didHighlightRowAtIndexPath:
- tableView:didUnhighlightRowAtIndexPath:
Try like this :
- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
return YES;
}
- (void)tableView:(UITableView*)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"Highlightimage"];
}
- (void)tableView:(UITableView*)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"image"];
}
You're trying to update the cell at the didSelectRowAtIndexPath function which returns void. So it will not update the tableview.
You should take the indexpath.row value from the diddSelectRowAtIndexPath and pass it to the cellForRowAtIndexPath. if they're matching, update the cell, it will work.
Also make sure you add a beginUpdate and endUpdate to reflect the table value reload

DetailTextLabel is not display in iOS 8 xcode 6

I making the tutorial on running iPhone 5S, Xcode 6 and iOS 8. I want to display the textDetailLabel in a cell table. Can you help me what's the problem here. I already checked the syntax but it doesn't work
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Checklist *checklist = [self.dataModel.lists objectAtIndex:indexPath.row];
cell.textLabel.text = checklist.name;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
int count = [checklist countUncheckedItems];
if ([checklist.items count] == 0) {
cell.detailTextLabel.text = #"(No Items)";
} else if (count == 0) {
cell.detailTextLabel.text = #"All Done!";
} else {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d Remaining", count];
}
return cell;
}
Move
static NSString *CellIdentifier = #"Cell";
out of the method, make it public.
When you create the table view write this line where you create the table (in viewDidLoad or your method where you create the table)
[dataTable registerClass:[UITableViewCell class] forCellReuseIdentifier:tableIdentifier];
Remove cell condition from
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
method because your table view cell is not nil and you can't set the cell style.
So you have to write
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

Why is my cell nil

I am writing the code to populate a UITableView from using a CoreData database wrapped in magical record. For some reason though the code docent seem to be working and my cell is considered nil. Why is this happening?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
PassWhizEntity *pazz = [allPasses objectAtIndex:indexPath.row];
cell.textLabel.text = pazz.destinationTeacherAttribute;
//cell.detailTextLabel.text = [self.dateFormatter stringFromDate:pazz.dateAttribute];
if (cell == nil)
{
NSLog(#"cell was nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
it just prints cell was nil and loads a blank UITable. Any ideas?
Your code should be like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSLog(#"cell was nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
PassWhizEntity *pazz = [allPasses objectAtIndex:indexPath.row];
cell.textLabel.text = pazz.destinationTeacherAttribute;
//cell.detailTextLabel.text = [self.dateFormatter stringFromDate:pazz.dateAttribute];
return cell;
}
You are trying to set textLabel of unallocated cell which have no effect on cell. Cell is returned nil because tableview don't have any cell that can be reused, so it is your responsibility to check for nil and make new Cell if needed.

Enter Data into Cell in UITableview

I have a small amount of data to show in a table and so I would like to load it all. Below is my code and for some reason nothing is showing up. Can you please have a look at what I have done wrong. Thanks
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
long row = [indexPath row];
cell.BBname.text = _BBNames [row];
cell.BBtype.text = _BBTypes [row];
cell.BBimage.image = [UIImage imageNamed:_BBImages [row]];
if([self getCheckedForIndex:indexPath.row]==YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;}
is "TableViewCell" your custom cell?
if not use "UITableViewCell" instead
if you are using interface builder for your custom cell, use initializer
[[NSBundle mainBundle] loadNibNamed: owner: options:]
also make sure your tableview delegate is set, specifically implement
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Iteration in UITableView for a particular cell

I have data and image in cells of UITableView. I want to change image of a particular cell,
I have a code with each cell.
I am not able to find the way to iterate the tableview for cell.
here is snap shot of my tableview function.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellText = nil;
NSArray *keys = [[[self packageItems] allKeys]
sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
cellText = [keys objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.imageView.image=[UIImage imageNamed:#"checkbox.png"];
//cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
return cell;
}
There is another function where i want to change cell image to cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
- (void)OnAction
{
static NSString *CellIdentifier = #"Cell";
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath ];
cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
}
If you wont to change something in table view with you function you dont have to make new cell, just modify actual cell. Just make condition in your cellForRowAtIndexPath delegate:
if(indexPath == indexToModify) {
cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
} else {
cell.imageView.image=[UIImage imageNamed:#"checkbox.png"];
}
and in your - (void)OnAction method use in the end:
[self.tableView reloadData];

Resources