Array of UITableViewCells to be reused in ios - ios

In cellForRowAtIndexPath: delegate n number of cells (corresponding to indexpath.row) are created and are added to an array cellsArray how to reuse each cell in the array, when cellsArray count is 7? (want to reuse the cells as a batch of 7)
FINameCell *cell = nil;
if ([FINameCellcells count] > indexPath.row)
{
cell = [FINameCellcells objectAtIndex:indexPath.row];
} else {
cell = [[[NSBundle mainBundle] loadNibNamed:#"FINameCell" owner:self options:nil] objectAtIndex:0];
[cellsArray addObject:cell];
}

You're not supposed to manually load cells from NIBs these days. The way to do that is to call UITableView's dequeueReusableCellWithReuseIdentifier:forIndexPath with the cell being either already defined as prototype cell in IB or by associating the cell via registerNib:forCellReuseIdentifier beforehand.
After that, don't worry about cell reuse, it's going to be all automatic.

Related

On scroll UITableViewCell added button image issue

I have UITableViewCell and added button left side of cell for check mark cell is selected or unselected.
Number of rows in uitableview 50.
Issue:
First row is selected and added the checkmark to button, then I scroll the UITableView, I found that another check mark is added to another cell.
Is any one face same issue, your input will be appreciated.
//Cell For Row IndexPath
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:UITableViewCell owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.checkButton.tag = indexPath.row;
// Image changes in Storyboard.
-(void)checkButtonAction:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
Reading your question , its the cell being re-used by iOS sir. The line below is the culprit.
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableCell];
What this does is look for an existing cell in the table view, and loads that cell.
You said, You clicked one cell and it had the checkmark, right?
Well, fortunately when you scroll away from it, the cell gets reused by the tableview by above call and is seen again in the list of visible cell with the checkmark.
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:UITableViewCell owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.checkButton.tag = indexPath.row;
Looking at your calls in the above method, you never set the checkmark to either on or off, sir. You are reusing the cell that already had the checkmark. So, when you see it again, it also has the checkmark, because you never changed it anyway.
NOTE: IT'S NOT RANDOMLY GIVING CHECKMARKS.
AT THE BEGINNING, DON'T SCROLL. GIVE CHECKMARK TO ALL THE CELLS AND YOU WOULD GET YOUR ANSWER. NOW START SCROLLING. Every cells should have checkmarks now.
Solution:
To solve this issue, in your datamodel, add a isCheckmarkSelected:Bool property.
And, anytime user checks the cell, make isCheckmarkSelected to true.
In the method call, [ cellForRowAtIndexPath ]... read the isCheckmarkSelected value and set the checkmark of the cell.
This would solve the not so random checkmarks issue.
Kind Regards,
Suman Adhikari
Click Here
This above link is worked fine for my question.
in button action add extra code [[self.tableview] reloadData];

Reloading data in tableView for invisible sections

Then I get json update I reload my tableView, but I have tableView with collectionView inside it and I have little lag in visible cell at the moment of reload. I want to reload only invisible cell of tableView. But there is no 'visibleSections' method like 'visibleCells'.
I get all visible sections
NSArray *visibleSections = [[self.tableView indexPathsForVisibleRows]valueForKey:#"section"];
Not I want get all sections, then remove all visibleSections and reload this new array. But I can not find the way to get all sections. How can I do it?
You could use one of these methods:
- (void)reloadRowsAtIndexPaths:withRowAnimation:
- (void)reloadSections:withRowAnimation:
Edit
When you try to get the indexPaths of all invisible cells, you could try something like this:
NSMutableArray *invisibleCells = [NSMutableArray array];
for (int i = 0; i < [tabsTableView numberOfSections]; i++) {
for (int n = 0; n < [tabsTableView numberOfRowsInSection:i]; n++) {
for (NSIndexPath *indexPath in [tabsTableView indexPathsForVisibleRows]) {
if ([indexPath section] == i && [indexPath row] == n) {
goto nextIteration;
}
}
[invisibleCells addObject:[NSIndexPath indexPathForRow:n inSection:i]];
nextIteration:;
}
}
More important is why you would like to do that?! Due UITableViewCells get reused, they won't be updatable. When the cell is "invisible", it changes its content and is used at a different position within the visible cells. This is how UITableView works.
You will (also currently) use this method to set the content of the cell:
- (void)tableView:tableView cellForRowAtIndexPath:indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_cellID];
if (!cell) {
cell = (UITableViewCell *)[[[NSBundle mainBundle] loadNibNamed:#"cellTemplate" owner:nil options:nil] objectAtIndex:0];
}
// configure cell content here
}
When the cell get's reused, this method gets called and you create the cell only if UITableView does not reuse an invisible cell. If it does, you change the content.
So there is no need to reload invisible cells and it's impossible. From the docs:
Return Value
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.

iOS: Customizing TableView cell to mimic music player

Is there an easy way of having a tableview cell like we see here with numbering like this and the border around. Is this created using different sections?
You need to create a custom UITableViewCell.
If you're using storyboards look here:
See this link http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
If not here is a rundown:
Basically create a new class that inherits from UITableViewCell and a XIB. Drag a UITableViewCell to the XIB and set it to the class that you created previously.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = #"CustomCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
//*- Load your custom XIB. objectAtIndex:0 says load the first item in the XIB. Should be your UITableViewCell that you dragged onto your XIB in Interface Builder.
cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] objectAtIndex:0];
}
//*- Customize the cell, i.e., cell.myLabel.text = #"Text";
return cell;
}
Using this technique you can layout your cell with three labels, one for the number and one for the name of the song and one for the song time. Add a background image view for the border and color.
A simple way to get the song number in the table is to use the indexpath.
cell.myLabel.text = [NSString stringWithFormat:#"%i", indexPath.row + 1];

Issue with UITextField values in custom cell while scrolling fast ios

I have a simple table with custom cells each containing a textfield. In cellForRowAtIndexPath: I create and initialize each cell depending on indexPath.row:
case 0:
{
CellIdentifier = #"TextEditCell";
TextEditCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
[cell configureCellWithText: [self.valueArray objectAtIndex:0]
placeholder: #"value no.0"]
[cell performAction: #selector(saveValue0:)
forControlEvent: UIControlEventEditingDidEnd
inTarget: self];
return cell;
}
configureCellWithText:placeholder: sets text and placeholder of cell's textField.
performAction:forControlEvent:inTarget refers directly to textField and saves the value of textField to local array to be accurate when used again.
Problem occurs, when I scroll the table fast. Values from different cells copy to another cells and modify local array. I can't find out why it happens. Anyone have any idea? I can provide more code if needed.
This is happening because you are reusing the cells and configureCellWithText is being run after the cell is reused. To solve this you could:
Don't reuse cells - But this would really hurt your performance.
If you are running on 6.0 you can use tableView:didEndDisplayingCell:forRowAtIndexPath: to cancel the text setting action when the cell scrolls off screen.
You can create a flag in your custom cell class that you set when you dequeue a cell.
Edit
Because I do not know how your cell works. It is hard for me to give you anything more then a sudo code concept.
Here is my sudo code:
Tableview Cell for row...
- dequeue cell
- [cell cancel_previous_action]
- set new actions.

IOS: Stop cells being incorrectly reused in UITableView

I have a basic UITableView with four sections. I control the content in each section with a Switch statement.
I programmatically create a button, which should appear in the rows of the first THREE sections, but should NOT appear in the fourth. However, the button is appearing randomly in the fourth section's rows.
I presume this is because a cell is being reused, but as I create each section's rows with the Switch statement, I cannot see how this is happening. Any ideas appreciated.
I am using a custom cell configured so:`
static NSString *CustomCellIdentifier = #"DashboardCell";
DashboardCell *cell = (DashboardCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DashboardCell"
owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[DashboardCell class]])
cell = (DashboardCell *)oneObject;
}
// Configure the cell.`
The code to create this button is: `
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(200, 11, 50, 50);
UIImage *iConnect = [UIImage imageNamed:#"connect.png"];
[button setImage:iConnect forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];`
You need a different reuse identifier for each type of content. So here you have two types of content - cell's that have a UIButton and cells that don't.
Use the indexPath of the tableView:cellForRowAtIndexPath: method to select a reuse identifier of either #"CellWithButton" or #"CellWithoutButton".
What is actually happening in your code is that all cells are given the same reuse identifier, meaning that they all get put into the same object pool. This means that when you use [tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; that you are retrieving a cell from this one pool (which potentially contains cells that have no UIButton and cells that do). Therefore the dequeue method can randomly return a cell that has already had a UIButton added to it. If you use two reuse identifiers, the UITableView will setup two object pools and will correctly deposit and retrieve the appropriate cells from each.
Or you can use one reuse pool and check the cell for a UIButton each time you retrieve one using the dequeue method.
In your DashboardCell, create a property #property (nonatomic, assign) BOOL buttonEnabled. Then in your awakeFromNib, always create the button and set button.hidden = YES. Override the setter of your property
- (void)setButtonEnabled:(BOOL)enabled {
buttonEnabled = enabled;
button.hidden = !enabled;
}
And finally override prepareForReuse
- (void)prepareForReuse {
[super prepareForReuse];
self.buttonEnabled = NO;
}
And now you can enbale/disable in your configure part of the method cellForRowAtIndexPath
You can use two different cell identifiers depending on the section. Otherwise you would need to see whether the button existed in the cell that's returned from dequeueReusableCellWithIdentifier: and add one or remove an existing one if necessary.
If you're going to be reusing these cells and there's some simple logic behind showing or hiding each cell's button the first thing I would recommend is that you create the button in Interface Builder and connect it as an outlet to your UITableViewDelegate.
I would then create a setup method for the cell that you can run at any time, any number of times without it breaking it:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CustomCellIdentifier = #"DashboardCell";
DashboardCell *cell = (DashboardCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DashboardCell"
owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[DashboardCell class]])
cell = (DashboardCell *)oneObject;
}
// Configure the cell.
[cell setupWithSomeConfigOptions:someConfigOptions]
return cell;
}
And in your cell subclass you would have the method -(void)setupWithSomeOptions:(SomeOptions)someOptions, which would be something along the lines of:
-(void)setupWithSomeOptions:(SomeOptions)someOptions
{
// some setup code
self.myButtonOutlet.hidden = someOptions.somePropertyToCheckIfButtonShouldBeHidden;
// some more setup code
}

Resources