My tableview works perfectly fine when not in editing mode. All cells show up as expected, but if I enter editing mode and scroll, the cells that are redrawn while in editing mode have the incorrect content. In my function that turns off editing, I reload the table data and it shows up correctly again.
Here the relevant code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
FieldItemDecrypted *theField = [decryptedArray objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = [[NSString alloc] initWithData:theField.field encoding:NSUTF8StringEncoding];
cell.detailTextLabel.text = [[NSString alloc] initWithData:theField.type encoding:NSUTF8StringEncoding];
return cell;
}
And my code for editing:
- (IBAction)editRows:(id)sender
{
if ([self.tableView isEditing])
{
[self.tableView setEditing:NO animated:YES];
[self.tableView reloadData];
}
else
{
[self.tableView setEditing:YES animated:YES];
}
}
Should look like this:
but looks like this after scrolling while editing:
You are initializing a UITableViewCell object first, then dequeuing from the table view? This is incorrect.
Try:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
I just tried this on a demo project and it behaves as expected.
I'm more familiar with this type of cell reuse:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:nil];
}
FieldItemDecrypted *theField = [decryptedArray objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = [[NSString alloc] initWithData:theField.field encoding:NSUTF8StringEncoding];
cell.detailTextLabel.text = [[NSString alloc] initWithData:theField.type encoding:NSUTF8StringEncoding];
return cell;
}
Related
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];
I have a UITableView with sections inside of them. All works great, but I can't seem to figure out how to customize the very fist index path. For example, I know this is very easily achievable through a normal tableview like so:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
if (indexPath.row == 0)
{
cell.detailTextLabel.text = #"My Custom text";
NSLog(#"%#",pm1.customText1);
}
}
}
I have tried the above implementation and it does not seem to work for me. Is there a different approach for section headers to achieve the same result with a normal tableview? Do I need to inject a "fake" section in my dictionary?
-(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];
}
if (indexPath.row == 0) {
cell.detailTextLabel.text = #"My Custom text";
NSLog(#"%#",pm1.customText1);
}
return cell;
}
Try this
The way you have it now the first cell is customize only when cell is nil.
You should move it out of the if statement.
- (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];
}
//check for first cell
if (indexPath.row == 0) {
cell.detailTextLabel.text = #"My first Cell";
NSLog(#"%#",pm1.customText1);
}else{
cell.detailTextLabel.text = #"Other cells";
NSLog(#"other cells");
}
return cell;
}
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.
What i am trying to do is parse an XML and have the elements populate the table view. The XML parses fine and prints in console, but is unable to display in the table view. Here is my code for populating a cell:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
[cell.textLabel setText:[news objectForKey:#"title"]];
[cell.detailTextLabel setText:[news objectForKey:#"link"]];
[cell.detailTextLabel setNumberOfLines:2];
}
return cell;
}
I got it to display. Seems I forgot to set the data source in the viewDidLoad method... thank you everyone for all the help.
You are only adding data to the text labels when you create a new one. Add the data to the view after the if statement.
UITableView will reuse cells when possible. When it does you will get an existing cell from dequeueReusableCellWithIdentifier:.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
}
NSMutableDictionary *news = (NSMutableDictionary*) [feeds objectAtIndex:indexPath.row];
cell.textLabel.text = [news objectForKey:#"title"];
cell.detailTextLabel.text = [news objectForKey:#"link"];
cell.detailTextLabel.numberOfLines = 2;
return cell;
}
Fill the data After creating creating or reusing your cell . It will work fine now ,Use below code.
-(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];
}
NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
[cell.textLabel setText:[news objectForKey:#"title"]];
[cell.detailTextLabel setText:[news objectForKey:#"link"]];
[cell.detailTextLabel setNumberOfLines:2];
return 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];