UITableCell displaying improper conten - ios

I am displaying a TabelCell that has images, button, UIView. When click the button it has to show UIView.
UIView is displaying but the problem is, if I click the button in first cell instead of displaying the UIView in the first cell, it is displaying the second cell or in some other cell. I don't know why this is happening.
The code is given below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"HomeTablecell";
cell = (HomeTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.vEdit.hidden=TRUE; //UIView hidden initially
[cell.editButton addTarget:self action:#selector(EditButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //When this button is clicked cell.vEdit become visible
}
-(void)EditButtonClicked:(UIButton*)sender
{
cell.vEdit.hidden=FALSE;
}

The answer for my question is FPPopover, when I use that control, the view is displaying exactly in the position I wanted
https://github.com/50pixels/FPPopover/

1: make sure the cell is referenced correctly(for instance I added tag to the button clicked)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"HomeTablecell";
cell = (HomeTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.vEdit.hidden=YES; //UIView hidden initially
cell.editBUtton.tag = indexPath.row //suppose has 1 section, you can customise the tag rule based on section/row
[cell.editButton addTarget:self action:#selector(EditButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //When this button is clicked cell.vEdit become visible
}
-(void)EditButtonClicked:(UIButton*)sender
{
HomeTableCell* theCell = self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0];
theCell.vEdit.hidden =NO;
}
2: Make sure UIView is hidden in your customised cell class for reused cell
//Your cell class
- (void)prepareForReuse {
[super prepareForReuse];
self.vEdit.hidden=YES;
}

Related

indexPath.row returns last visible cell, and didSelectRowAtIndexPath not being called

I have an NSArray and a tableView with some buttons whose title is the string in the array at the current indexpath row
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath {
_selectedRow = indexpath.row;
static NSString *simpleTableIdentifier = #"customCell";
cell = [myTableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
[cell.playButton setTitle:array[indexpath.row] forState: UIControlStateNormal];
return cell;
}
Button titles are well shown.
Now I have some mp3 files whose names are the same as the strings in the array, and I want to play the file corresponding to the selected cell.
fileToPlay = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#", array[_selectedRow]]; ofType:#"mp3"];
What's happening here is that the file played is always the one corresponding to the last visible cell in the table.
I also have
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexpath {
_selectedRow = indexpath.row;
}
but If I try to print _selectedRow here, nothing appears in the log.
When I click on a cell in the table, it doesn't seem selected (it's not gray colored).
dataSource and delegate are also well connected to the tableview.
UPDATE
I found out that if I click on the button, it's like I'm not clicking on the selected row. if I click on the cell (outside the button), the indexPath.row is correct.
Remove the following line from the cellForRowAtIndexPath method.
_selectedRow = indexpath.row;
You are setting the _selectedRow value each time the cellForRowAtIndexPath is called,which is when each cell is drawn and explains why the value of the last cell is being taken.
Set Code:
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath {
cell.playButton.tag = indexpath.row;
[cell.playButton addTarget:self action:#selector(btnPlay:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)btnPlay:(id)sender{
UIButton *btn = (UIButton *)sender;
_selectedRow = btn.tag;
}
You are select selection color for touch color set.
Try these:
1) set myGestureRecognizer.cancelsTouchInView to false... maybe your touches are getting in way. (It's a common issue when you may have gesture recognizers)
2) In your tableView's attribute's inspector, set Selection to singleton. That solved my issue
3) set this : [tableView setAllowsSelection:YES];

Tableview button click need pickerview in ios

Creating the UIpickerview in tableview button click. When click the each button first time the picker want to show . if i click the same button in another time picker want to hide and get the picker value. here i put my code only the last cell index done the process the first cell index is not working? how can i fix this issues help me!!! here screen!.
create the picker in global like
UIPickerView *myPickerView;` and acess in tableview
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_arr_tags count]; /// in tag array number of button count
}
-(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:nil];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
/// create a button
UIImage *ButtonImagecmt = [UIImage imageNamed:#"orangeButton#2x.png"];
UIButton *myButtoncmt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButtoncmt.frame = CGRectMake(10,50,90,35);
[myButtoncmt setBackgroundImage:ButtonImagecmt forState:UIControlStateNormal];
[myButtoncmt addTarget:self action:#selector(picker_select:)forControlEvents:UIControlEventTouchDown];
[cell addSubview:myButtoncmt];
flg=1; /// first time picker want to hide so flag set 1.
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(120, 0, 160, 180)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
myPickerView.hidden=true;
[cell addSubview:myPickerView];
}
return cell;
}
- (IBAction)picker_select:(id)search
{
if (flg==1)
{
/// first time button clickt picker want to display
myPickerView.hidden=false;
flg=0;
}
else
{
/// secound time button click picker want to hide
myPickerView.hidden=true;
flg=1;
}
}
This code only working in last cell. want to work in all the cell in table view
First let me explain your error after that I will suggest you other way.
You are declaring UIPickerView *myPickerView; in out side of following method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
But you are initializing myPickerView inside of following method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and again you are trying to access myPickerView outside of the following method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
simply the object myPickerView represents last cell pickerview. so what ever button you tapped in table view, myPickerView always represents last cell pickerview. As per reusability concept, you can't see this picker until you are loading last cell on screen.
so what I am suggesting you to work with your code
set index row as tag to your button like bellow in cellForRowAtIndexPath method
myButtoncmt.tag = indexpath.row;
now in the same way set tag to your pickerview also(I am not recommended this but I am trying to resolve issue with your code ) like bellow
myPickerView.tag = [_arr_tags count]+indexpath.row;
Now in - (IBAction)picker_select:(id)search access that using following code
UIButton *tempBtn =(UIButton *) search;
UITableViewCell *cell= [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tempBtn.tag inSection:0]]
UIPickerView *pickerView = [cell viewWithTag:[_arr_tags count]+tempBtn.tag];
pickerView.hidden = !pickerView.isHidden;
Now you can see all pickers. try and let me know your result

Clicking UIButton in UITableView clicks on multiple buttons

When I select one button in my UItableView other buttons lower down are also clicked. I am using target action on a button in a custom tableview and changing the title of sender in the method. Why is this clicking one button selecting multiple UIButtons? Any help/suggestions would be greatly appreciated.
EDIT: I am using a sectioned tableview
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// Configure the cell...
[cell.likeButton addTarget:self action:#selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
cell.likeButton.tag = indexPath.section;
return cell;
}
-(void)likeButtonPressed:(UIButton *)sender {
NSLog(#"%d",sender.tag);
[sender setTitle:#"Pressed" forState:UIControlStateNormal];
}
In your PostTableViewCell.h define a Delegate
#protocol HandleCellInteractionDelegate <NSObject>
-(void) didPressButton:(NSString*)action forCell:(UITableViewCell *)theCell;
#end
In TableView.h register as a Delegate for this protocol
<HandleCellInteractionDelegate>
In TableView.m file add the delegate handler
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Create cell
....
cell.delegate = self;
if (self.qObjects current object's selection state == selected)
enable button
else
disable button
}
Add the function for protocol
#pragma mark HandleCellInteractionDelegate
-(void) didPressButton:(NSString*)action forCell:(UITableViewCell *)theCell{
// Now here you have access to your cell again. And you can update the cell's button text as needed
// Here save selection state in data model.. in self.qObjects <==
cell.questionTextView.text = #"bla bla";
}
In your uitableviewcell subclass, make sure you are only adding a button to the cell during init. Otherwise you will add an additional button to the cell each time the tableviewcontroller calls dequeueReusableCellWithIdentifier.
Try This:
// Add a property to your custom cell to hold the indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// Configure the cell...
cell.indexPath = indexPath;
return cell;
}
And in your cell subclass wire up an action method, something like this:
- (IBAction)buttonAction:(id)sender {
NSLog(#"indexPath: %#", self.indexPath);
}
i think you have Multiple Rows in a section so all button of that section get selected
use
cell.likeButton.tag = indexPath.row;
instead of
cell.likeButton.tag = indexPath.section;

Disabling a UIButton in a UITableViewCell with cell reusability

I have a UIButton in each of my UITableViewCells. When the button is pressed I am disabling it so that the user cannot press it again (it is a like button). However, when the user scrolls passed a cell and the scrolls back to the cell the button is selectable once again. I'm guessing this is because the cell is redrawn when the user comes back to it, resetting the button. Is there a way I can avoid this? My code is below
Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifer = #"cellName";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
}
UIButton *ilikeit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[ilikeit addTarget:self action:#selector(like:) forControlEvents:UIControlEventTouchUpInside];
ilikeit.frame = CGRectMake(55, h+70, 45, 25);
[cell addSubview:ilikeit];
return cell;
}
-(void) like:(id) sender {
((UIButton *)sender).enabled = NO;
}
You can store the button state in your data model for the tableview class.
Say the TableView is loading data from a class MyData which looks like this:
#interface MyData: NSObject
// Your other data here such as strings etc
NSString *otherData;
// Here add a selected flag
BOOL selected;
#end
in the TableViewDelegate, read and write to this model.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyData *thisCellData = [yourGlobalMyDataArray objectAtIndex:indexPath.row];
// set other data
if (thisCellData.selected){
// Hide the button
// Do other stuff as needed for this button
}
else {
// Show the button
// Do other stuff as needed for this button
}
}
The same data model should be updated when user selects the button.
Hope this helps.
Problem- 1 You are creating your button every time cellForRowAtIndexPath gets called.
Problem- 2 You have no code to keep the state of your button with cell reusability, that's why it is refreshed when cell is going for reuse.
Solution- For this you have to keep your selected button state. Make a global NSInteger variable. And set its value as sender.tag value in like: method. And use -
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
// here create your UIButton
// Set its tag to indexPath.row
// add this to cell as subview
}
// find button of current cell using tag
UIButton *btn = (UIButton*)[cell.contentView viewWithTag:indexPath.row];
// place your check here if button tag matches with selected tag then disable btn here.
// Else use setEnable with YES parameter.
}

Changing UIButton title to selected table cell

I want to change a UIButton text to the selected cell in a UITableView. Normally this would be done like this.
TableView.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if([self.delegate respondsToSelector:#selector(ButtonText:)]) {
[self.delegate ButtonText:selCell.textLabel.text];
}
}
ViewController.m:
- (void)dateSpecifiedButtonText:strText2
{
[self.dateSpecifiedButton setTitle:strText2 forState:UIControlStateNormal];
}
This is working when I select the cell but what I want is to change the button to the cell when you click on another button. So I need to rewrite this method without using didSelectRowAtIndex but I have no idea how.
If you know the indexPath of the cell in which button is
UITableViewCell *cell = [self.tableView cellForIndexPath:indexPath];
If you have just added button to contentView of cell give a unique tag to the button
UIButton *button = (UIButton *)[cell.contentView viewForTag:55];
Now you have the instance of button, set the text to it.

Resources