On scroll UITableViewCell added button image issue - ios

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];

Related

UITableView - reusing cells while in editing mode

I'm finding that if you set a table view into editing mode, upon scrolling the table after deleting a row the cell edit control (the red minus sign on the left) is in a random state (turned vertical or horizontal) on the rest of the cells as you scroll the table. Presumably because I'm reusing cells like this:
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
How can I force the edit control to be in the correct state for each cell? It should always be in the default horizontal state unless I tap it to delete a cell.
EDIT: Here's the cell code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"WhateverIdentifier";
MyCell *cell = nil;
//This IF statement fixes the problem, but then I'm not reusing the cells
if (!tableView.isEditing) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil] objectAtIndex:0];
}
//customize cell
return cell;
}
Are you calling [super prepareForReuse] in the method prepareForReuse of your custom cell?
That resolved my problem.
I just checked in a UITableView I had handy, and I don't see that problem. I'm using dequeueReusableCellWithIdentifier: as you are (without the if statement, of course). Are you doing something special with swiping, or deleting multiple rows or something?
(I'd make this a comment but can't yet. I'll delete it once you've resolved your problem.)

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
}

Why is my custom UITableViewCell not showing?

I've done custom UITableViewCells before without issue.. but I can't figure out what is going on with my current project.
Here's what I've done...
Create CustomCell.h (subclassing UITableViewCell)
Create an empty XIB and drag in UITableViewCell. Set background to black.
Change class of UITableViewCell in Interface Builder to "CustomCell"
Import CustomCell.h in my DetailViewController
Modify tableView:cellForRowAtIndexPath:
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"DO I GET HERE?");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
I would expect my tableview cells to show as black? But they are still showing as white... any ideas? Thanks!
Update: Ok, turns out, it is loading the custom cell... I added a UILabel with some white text. I couldn't see it, but when I highlighted the cell I could see the text was there. So now the question becomes, why is the cell ignoring the black background I have set for the cell?
EDIT: As for why it's not black - I expect there is something obscuring your black - the likeliest candidate for this is the label background being white and not clear.
As well as point 3.
The attributes inspector (the 4th tab) needs to have the reuse identifier set to the identifier you are going to reuse (you use #"cell" in your question). I would try and use something a bit more specific - after all in some apps you might have many types of custom cells.
I think you also need to cast the topLevelObjects to (CustomCell*) thus
if (cell == nil) {
NSLog(#"DO I GET HERE?");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
cell = (CustomCell*)[topLevelObjects objectAtIndex:0];
}
Seems to ignore the background colour I've set, so I just add a UIView to it with a background colour and that seems to work..
You have to register your custom cell with the tableview
This needs to happen before that delegate is called:
[self.tableView registerClass: [CustomCell class] forCellReuseIdentifier:#"CellIdentifier"];

Resources