I've embedded horizontal table views inside of table cells (ala the Pulse Reader). The interesting behavior that I'm getting is that dequeueReusableCellWithIdentifier: seems to be correctly remembering the offset of the embedded table views, without me doing anything. But there are two flavors to "remembering"
First (expected)
I create a table with 100 sections and 1 row per section. Each cell (each section) gets an embedded table view that I force to have 100 rows and 1 section. When I scroll the vertical table view, cells are reused (checked by looking at the instance names of the cells after dequeueReusableCellWithIdentifier:
VerticalTableViewCell *cell = (VerticalTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
If I scroll the first table view cell's embedded table view to the right, say 10.5 cells, when I scroll down a couple cells in the vertical table view, the reused table cell has its embedded table view offset by those 10.5 cells. This makes sense, the cell was just reused and I didn't reset any offsets. Let's say that reused cell was the 7th row. If I now slide the 7th row's embedded table view (this is the same embedded table view as row one, due to the reuse) to position 20, when I go to the top of the vertical table view that embedded table view (which I had originally moved to 10.5), is now at 20. Again, expected, those table cells are the same. Just reused.
Second (desired, but have no idea how it works)
Now I fill in my real data (instead of just printing
[cell.textLabel setText:[NSString stringWithFormat: #"here: %d", indexPath.row]];
to the cell.
Now, all of a sudden, my embedded table views DO remember where they were. I once again ensure the cells are being reused (same method as above). But this time, when I scroll the first embedded table view (to position 10.5) and scroll down to the seventh row, the seventh row is at its starting point. When I scroll the first row back into view, it's right where I left it.
This is using Xcode 4.2 and iOS 5.0. Is there something smart that dequeueReusableCellWithIdentifier: does?
In old school (pre-iOS 5.0), I would have checked the
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
bits of logic to see what was happening, but with StoryBoards those are no longer necessary.
The 2 sample projects that behave differently are kind of big, but if there's any relevant code you'd like to see, let me know.
Sorry to be so vague, I'm just trying to grasp exactly whats going on with the memory management of these table views.
All the connections are via IBOutlets and made in the StoryBoard.
Thanks!
First Vertical Table View Controller
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PlayerCell";
PlayerCell *cell = (PlayerCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.games = [NSMutableArray arrayWithCapacity:1];
if(indexPath.section <40){
//[cell.textLabel setText:[NSString stringWithFormat:#"h343: %d", indexPath.section]];
}
CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
cell.htv.transform = rotateTable;
cell.htv.frame = CGRectMake(0, 0, 320, 120);
return cell;
}
First Horizontal Table View Delegate/Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
cell.transform = rotateImage;
//[cell.myLabel setText:[NSString stringWithFormat:#"%d", indexPath.row]];
[cell.textLabel setText:[NSString stringWithFormat: #"here: %d", indexPath.row]];
return cell;
}
Second Vertical Table View Controller
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [self.leagues count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"VerticalTableViewCell";
VerticalTableViewCell *cell = (VerticalTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(#"Cell: %# for index: %d", cell, indexPath.section);
cell.games = [self.leagues objectAtIndex:indexPath.section];
CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
cell.horizontalTableView.transform = rotateTable;
cell.horizontalTableView.frame = CGRectMake(0, 0, 320, 120);
// Configure the cell...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.section = indexPath.section;
return cell;
}
Second Horizontal Table View Delegate/Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.games count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HorizontalTableViewCell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
GameTableCell *cell = (GameTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
cell.transform = rotateImage;
//[cell.myLabel setText:[NSString stringWithFormat:#"%d", indexPath.row]];
NSDictionary *game = [self.games objectAtIndex:indexPath.row];
[cell.homeTeamLogo setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",[game objectForKey:#"LogoImage_HomeTeam"]]]];
[cell.visitingTeamLogo setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",[game objectForKey:#"LogoImage_VisitingTeam"]]]];
[cell.homeTeamName setText:[NSString stringWithFormat:#"%#", [game objectForKey:#"AbbreviatedName_HomeTeam"]]];
[cell.visitingTeamName setText:[NSString stringWithFormat:#"%#", [game objectForKey:#"AbbreviatedName_VisitingTeam"]]];
NSDictionary *gameTime = [game objectForKey:#"GameTime"];
NSString *minutes = [NSString stringWithFormat:#"%#", [gameTime objectForKey:#"Minutes"]];
if([minutes length]==1){
minutes = #"00";
}
NSString *timeOfGame = [NSString stringWithFormat:#"%#:%#",[gameTime objectForKey:#"Hours"], minutes];
[cell.gameTime setText:timeOfGame];
return cell;
}
My fault.
I was saving the state of the the embedded Table View.
FYI: I was using the tableView.contentOffset (in case anybody needs to do this later).
Whoops.
Related
I'm developing an iOS 7+ app, and I've some UITableViewController in storyboard that are showing a weird behavior. I've a basic prototype cell defined in one of them, with an identifier #"standardCell" also set in storyboard. In the associated UITableViewController class, I've this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%d", indexPath.row];
return cell;
}
Cells are loaded the first tiem the table view is shown, but as soon as I scroll the table content, all cell titles that were set appear empty and cellForRowAtIndexPath: is not called anymore. The didSelectRowAtIndexPath: delegate method is neither called.
I've set both delegate and dataSource for this table view to be the table view controller. And its .h file conforms to UITableViewController <UITableViewDataSource, UITableViewDelegate>.
I find a similar issue with another table view and associated view controller, where prototype cell is a custom cell instead: cells show wrong data and weird content when I scroll the table, as if cells where not being dequeued and reused as expected.
What could I being missing?
Thanks
at least in this method:
Change this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%d", indexPath.row];
return cell;
}
To this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"standardCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%d", indexPath.row];
return cell;
}
I have a problem with data being displayed in a UITableViewCell I have created.
I have a CoreData Model which I have a custom Cell which I am loading from a Xib. The layout loads correctly and the correct number of cells are generated. The problem comes when updating the button.titleLabel.text and description.
Here is my uitableview and uitableviewcell related code:
#pragma mark - UITableViewDatasource Methods.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [pointsHistoryItems count];
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d",indexPath.row];
PointsHistoryItemCell* cell = (PointsHistoryItemCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return [self createCustomCell:cell cellForRowAtIndexPath:indexPath];
}
- (PointsHistoryItemCell *)createCustomCell:(PointsHistoryItemCell *)cell cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil)
{
cell = (PointsHistoryItemCell *)[[[NSBundle mainBundle] loadNibNamed:#"PointsHistoryItemCell" owner:self options:nil] objectAtIndex:0];
}
AwesomePointsHistoryItem *aphi = (AwesomePointsHistoryItem *)[pointsHistoryItems objectAtIndex:indexPath.row];
cell.description.text = aphi.description;
NSString* pointsText = [NSString stringWithFormat:#"%# Points", [aphi.points stringValue]];
[cell.button.titleLabel setText:pointsText];
NSLog(#"is reaching createCustomCell");
NSLog(#"points %#", cell.button.titleLabel.text);
return cell;
}
the log prints:
is reaching createCustomCell
points 600 Points
However.. The cell.button.titleLabel.text does not update!
What am I doing wrong?
Use
[cell.button setTitle: pointsText forState: UIControlStateNormal];
instead of
[cell.button.titleLabel setText:pointsText];
Note : hope cell.button is a UIButton
I'm basically making a settings view on my app and I'm using a static table for it. So I divided the table into 3 sections each with one cell. Not programmatically I can easily label each cell and it works but programmatically I'm not able to initialize each cell. I can only initialize the first cell that gets repeated across the 3 sections. I would like a way to initialize a cell for each section but I can't find a method or a way to do that. My tableview also has reuseIdentifiers but it doesn't seem like the UITableViewController recognizes it.
This is what I have done so far.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EditProfile";
//UITableViewCell *cell = nil; //[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// More initializations if needed.
}
//Observations *currentList = [self.teacherNames objectAtIndex:indexPath.row];
cell.textLabel.text = #"Hey";
//return cell;
return cell;
}
But what I want is the cell in the first section to be labeled: Edit Profile, the second one: Invite and the third:Logout
You have to make a condition for how to handle each different section within the table view.
if (indexPath.section == 0) {
cell.textLabel.text = #"Edit Profile";
}else if (indexPath.section == 1) {
cell.textLabel.text = #"Invite";
}else if (indexPath.section == 2) {
cell.textLabel.text = #"Log out";
}......
Check out the tableView delegate methods:
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
(IF you're wanting to label the section headers and not the cells in the sections themselves)
I've stucked to this problem for couple days, and I've debugged it many times and can't figure out what's going wrong.
I have a UITableView that has three sections. I'd like to add buttons as accessoryView and UILabel as detailText to the second section.
It looked OK when View first loaded.
But after scroll it, it becomes
After couple scrolls, it freezes (not crash).
Below is my code, any advise will be appreciated.
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return self.status.count;
}
else if (section==1)
return self.overrideCtrl.count;
else
return self.levelStatus.count;
// Return the number of rows in the section.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(#"section %#",indexPath);
if (indexPath.section == 0) {
cell.textLabel.text = [self.status objectAtIndex:indexPath.row];
NSLog(#"Status %#",cell.textLabel.text);
}
else if (indexPath.section == 1) {
cell.textLabel.text = [self.overrideCtrl objectAtIndex:indexPath.row];
cell.detailTextLabel.text =[NSString stringWithFormat:#"00:%02d",[[self.timeLeftArray objectAtIndex:indexPath.row] intValue]];//[[self.timeLeftArray objectAtIndex:indexPath.row] stringValue];
cell.accessoryView=[self.checkButtonArray objectAtIndex:indexPath.row];
NSLog(#"override %#",cell.textLabel.text);
}else{
cell.textLabel.text = [self.levelStatus objectAtIndex:indexPath.row];
NSLog(#"level %#",cell.textLabel.text);
}
return cell;
}
Thanks!
This problem is covered a lot on SO, it's because of cell reuse. You need to set the accessoryView to UITableViewCellAccessoryNone for your other sections, otherwise, a cell that was used for section one might be used in another section, and still have its accessory view.
You only have 1 CellIdentifier. You should have a different one for each section.
i have a uitableview with custom cells.. with normal code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DDMainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[DDMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
the problem is when i select one cell i add progress bar on the cell that download data online.. but when i Scroll down i find that every 10 cells have the same progress bar .. how can i prevent this behavior ?
Try this,
- (void)viewDidLoad
{
[super viewDidLoad];
dataarr=[[NSMutableArray alloc]init];
indexarr=[[NSMutableArray alloc]init];
mytableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
mytableview.dataSource=self;
mytableview.delegate=self;
[self.view addSubview:mytableview];
for (int i=0; i<30; i++) {
[dataarr addObject:[NSString stringWithFormat:#"%d",i]];
}
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [dataarr count];
}
- (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] ;
}
cell.textLabel.text=[dataarr objectAtIndex:indexPath.row];
UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[act setFrame:CGRectMake(50, 20, 20, 20)];
act.hidden=YES;
[cell.contentView addSubview:act];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]])
{
[act startAnimating];
act.hidden=NO;
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]])
{
[mytableview reloadData];
return;
}
[indexarr addObject:[dataarr objectAtIndex:indexPath.row]];
[mytableview reloadData];
}
Make sure, when downloading is complete, then remove this object from indexarr....
That's because your cells are getting reused; UITableView will put off-screen cells into the reusable cell queue, and dequeue them for reuse if the reuseIdentifier matches. You should use some other data structure (e.g. NSArray or NSDictionary) to track which indices/cells have already been tapped. Then, in the method you showed above, regardless of whether the cell was init-ed or dequeued, set the progress bar according to your underlying data structure.
Here your used UITableViewCellIdentifier is reuseIdentifier. Which will work for all Cells are same type. Now your are taking once cell with progress bar. Now it will different from all cells data.
So use one more tableview cell for progress bar, or while reloading table remove the Progress bar which is exists already and add again. for this use tag for progress bar.