I need to literally redraw a UITableView upon an event in my code. But everything I've tryed doesn't seem to work. I've tried [self.tableView reloadData], I've played with the delegates. The goal I'm trying to achieve is to render a completely different UITableView, with differently formatted cells. So I need to redraw the table. Any help would be appreciated.
Heres my code:
...
if/else...
}
//Now I want to reload the tableView
[tableView reloadData]; //Isn't getting it done
NSLog(#"index = %i", index); //Always fires
tableView.delegate = self; // Didn't help
[tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO]; // Also nothing
}
The point of what I'm trying to do is this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (segmentOptions == snacks) {
cell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(#"A");
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSLog(#"B");
}
}
...
}
You possibly meant something like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell-not-snacks";
if (segmentOptions == snacks) {
cellIdentifier = #"cell-snacks";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
if (segmentOptions == snacks) {
cell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
}
}
I think it would be worth looking at the method prepareForReuse in the UITableViewCell Class. This method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:.
You can override this method on your custom UITableViewCell (do not forget including [super prepareForReuse]) in order to redraw whatever you need to redraw.
The problem is: if (cell == nil) {. I removed that and it works perfectly every time.
Related
I am new in iPhone development. I am using UItableview for showing list.
And able to show all items successfully.
But they changed when I scroll tableview.
Please help me.
Try this code in cellForRowAtIndexPath method of tableview
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Please give me more information to show your case clearly.
Have a look at this function first because all rows are generated from here!
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;
}
Check whether each row is exist,unless just reuse it later.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
UITableViewCell *cellName=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell1"];
NSDictionary *contentdict=[innerarray objectAtIndex:indexPath.row];
static NSString *cellidentify=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentify];
}
this my code every thing is working but the crash appears when i scroll the tableview
even i have given the array value correctly and given the delegate and datasource as self
is there any thing i need to write r change ?????
i am using Xcode 4.5
Try this...may be it help....
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"] autorelease];
}
NSDictionary *contentdict = [[NSDictionary alloc]init];
if([innerarray count]>0)
{
contentdict =[innerarray objectAtIndex:indexPath.row];
}
// Write your other cell stuff....
return cell;
}
I think it's crashing because you are not returning any cells from the cellForRowAtIndexPath method. Change cellForRowAtIndexPath like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"] autorelease];
}
//do something with the cell
return cell;
}
I am having issues with what I can only imagine is a very simple problem. I am loading a UITableViewController class called LocationViewController from one UIViewController:
LocationViewController *lvc = [[LocationViewController alloc] init];
[self.navigationController pushViewController:lvc animated:true];
In this class I have the 3 following method implemented:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CityCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = #"Test";
return cell;
}
And I am getting the following error:
UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I have had this error before when transitioning between ViewControllers using the StoryBoard this was because the CellIdentifier was not correct. I have no idea what I am doing wrong. I have tried loading a nib file with this ViewController but that throws the same error.
You have to allocate the cell. use this code.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
use this 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] autorelease];
}
cell.textLabel.text = #"Test";
return cell;}
dequeueReusableCellWithIdentifier returns nil, if there is no object in the reusable-cell queue. check if cell is nil after calling dequeueReusableCellWithIdentifier, create a new UITableViewCell in that case.
dequeueReusableCellWithIdentifier is a cache of already created UITableViewCells with your identifier "CityCell"
If you use the code below, it tries to get a cell from the cache, and if it can't it will create one and then store it in the cell cache for later use.
You need the cache for large tables, as it drastically improves table performance when scrolling.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"CityCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.textLabel.text = #"Test";
return cell;
}
Take a look at the table view programming guide to learn more about this:
Table Programming Guide
How can you turn the blue selector to go off when I move back into the view? At the moment, when I select it, I get pushed to another view. However, when I go back into the original view, it is still selected.
How can I turn it off when I go back onto the original view?
Thanks.
In your tableView:didSelectRowAtIndexPath: method you should include the following:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
You can also try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"myCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Set up the cell...
cell.textLabel.text = #"foo";
return cell;
}
I "always" deselect the rows when the view disappears:
-(void)viewDidDisappear:(BOOL)animated {
int i = 0;
while (YES) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i++ inSection:0];
if ([yourTable cellForRowAtIndexPath:path] == nil) { break; }
[yourTable deselectRowAtIndexPath:path animated:NO];
}
}
Haven't checked the code
for some reason this UITableView keeps crashing and it seems like the UITableView is unable to display the secondsString for some reason but i have no idea why... please help me out here cause i have no idea what i could be doing wrong..... Thanks!!
#import "SettingsView.h"
#implementation SettingsView
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
transmissionSetup = [[TransmissionSetupViewController alloc] initWithNibName:#"TransmissionSetupViewController" bundle:nil];
transmissionSetup.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
NSLog(#"%i", [self getMinutes]);
[self setSeconds:10];
secondsString = [NSString stringWithFormat:#"%i", [self getSeconds]];
[self.tableView reloadData];
}
#pragma mark Table view methods
// 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:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
cell.textLabel.text = #"Every:";
cell.detailTextLabel.text = secondsString;
}
// Set up the cell...
return cell;
}
#end
You initialize your secondString with autoreleased string - so it is not guaranteed that it will be valid outside current scope. You need to retain it upon initialization (and don't forget to release later)
I would advise you to have a look at objective-c properties for accessing ivars.
// try this
- (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];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
cell.textLabel.text = #"Every:";
cell.detailTextLabel.text = secondsString;
}
// Set up the cell...
return cell;
}