Odd question, I am creating a UITableView, setting 8 rows of data. Simply, I'm just returning the indexpath row to try and figure out what is wrong, but ultimately, any rows that are off screen seem to load in a strange order.
For example, rows 0,1,2,3,4,5 all appear ok on first load, but then 6 & 7 are off screen. When I scroll to them I usually see rows 6 and 0, or 6 and 1. I then see row 7 appear back at the top of the table when I scroll back up.
Here's my code:
- (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.textLabel setText:[NSString stringWithFormat:#"Row: %d", [indexPath row]]];
}
return cell;
}
Am I doing something really stupid? Cheers
You can use
NSString *cellIdentifier =[NSString stringWithFormat:#"%d",indexPath.row];
instead of
static NSString *CellIdentifier = #"Cell";
Create your cell in the if statement and then set the properties (ie the label) outside the if statement.
Related
I have a UITableView with a custom cell using Storyboards. All the objects inside the custom cell work but when I scroll in my UITableView the objects do not move:
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MainCell";
MainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.subtitle.text = [NSString stringWithFormat:#"%#",theEvent.startDate];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Any ideas on what I have done wrong? Thanks.
I figured out the problem. The objects that I had placed on the UITableViewCell were not actually inside the cell but on top of the UITableView and therefore they were acting as "floating" objects.
I have a table with a few rows, and the place holder text for all is the same. How can I programmatically change the placeholder text of only the first one?
Thanks!
You need to run an if statement to check the index of the cell. 0 is the first index, so you would need to check to see if the row is 0.
- (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];
}
if (indexPath.row == 0)
cell.textLabel.text = #"First Cell";
else
cell.textLabel.text = #"Not First Cell";
}
In order to access your cell AFTER the cell was created/reused, try the - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath method of your uitableview object! See the following Apple doc for a detailed elaboration on this method.
Note:
Return Value
An object representing a cell of the table or nil if the cell is not
visible or indexPath is out of range.
I have a tableview with 5 sections and variable number of rows in each section with a custom cell (for check and uncheck images on a cell). When I select a row in first section, tableview also selects any one of the row in the last section.
And when I scroll up and down, the checked rows are getting shifted randomly with in the sections.
The issue is that it's not there when I remove one section in the tableview. I.e even number of sections.
Can anyone help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = #"MyCellID";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = (CustomCell *) [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID]autorelease];
}
That is the code that I used.
I'm sure somewhere I did a mistake. But, I'm not able to find it.
this is because of reusing of cells in the table, read more about table reusing methodology.
To go in detail the changes made to a cell will be copied to others since the cells are reused to viewable cells.
We will need to see some code to properly diagnose the problem, specifically the implementation of - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {.
Are you correctly reusing the cells as specified in apples docs i.e.
static NSString *cellIdentifier = #"MyAwsomeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
I have a UITableView, and I am showng data based on indexPath.section, however, when I scroll my table view very quickly, its data keep overlapping. How to fix this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch(indexPath.section)
{
// case 0 to 10;
//values change if I scroll my table
}
}
I think you are allocating some labels inside the cellForRowAtIndex delegate method. Thats why you got this problem. You can solve this in 2 ways:
Alloc the labels outside the delegate
method. set tags for your labels and
use it inside the cellForRowAtIndex
by refering the tags.
use custom cell view controller.
i fixed the issues
here is the solution if any one need
NSString *CellIdentifier=nil ;
NSMutableArray *Array= [[[NSMutableArray alloc] initWithObjects: #"One",#"Two", #"Three",#"Ad",#"Ae",#"Ah",#"Aj" ,nil]autorelease];
CellIdentifier = [Array objectAtIndex:indexPath.section];
///302-1021-9244-4658-1994-3384
UITableViewCell * cell = [tabelView dequeueReusableCellWithIdentifier:CellIdentifier];
Thanks
I want to make a table view in my iPhone Application.. In that table each shall contains 1 image, some text of line, some symbols, etc.. I don't now how to customize our cell of table. Please provide some help if you have..
I think you can use a default UITableViewCell to do what you described by initializing the Cell (in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ) with the proper UITableViewCellStyle
e.g.:
-(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];
}
// Set up the cell...
return cell;
}
which gives you:
"A style for a cell with a left-aligned label across the top and a left-aligned label below it in smaller gray text. The iPod application uses cells in this style."
Also every kind of UITableViewCell has an image-property by default.
but here are some tutorials for building custom cells:
Custom UITableViewCells with Interface Builder
http://iphone.zcentric.com/2008/08/05/custom-uitableviewcell/
You can create a cell in Interface Builder. Start with a UITableViewCell which acts like a view, and then add a UIImage, one or more labels, whatever containers to hold your symbols, etc.
Then in your cellForRowAtIndexPath, load the nib and fill in the picture and labels, etc.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
MyTableCell *cell = (MyTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = myTableCell; // get the object from the outlet
myTableCell = nil; // make sure this can't be re-used accidentally
}
// Set up the cell...
cell.myViewController = self;
// set up image and labels here
return cell;
}