I am using a custom UITableViewCell with a textfield. I want to access each textfield's data when I press on the right navigation bar button (e.g:Done,etc). I am using following code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
#pragma mark - UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] objectAtIndex:0];
if (cell == nil) {
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.valueTextField.delegate = self;
switch (indexPath.row) {
case 0:
cell.itemImageView.image = [UIImage imageNamed:#"user.png"];
cell.valueTextField.placeholder = #"UserName";
break;
case 1:
cell.itemImageView.image = [UIImage imageNamed:#"mail.png"];
cell.valueTextField.placeholder = #"Email";
default:
break;
}
Try this
-(IBAction)rightNavigationButton:(id)sender
{
NSInteger rowNumber = [projectListTable numberOfRowsInSection:0];
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:[projectListTable numberOfRowsInSection:0] inSection:0];
for(int i = 0; i < rowNumber; i++)
{
CustomCell *cell = [projectListTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowNumber inSection:0]];
NSString *str = cell.valueTextField.text;
//Do here what you want to do with textfield of each cell
}
}
I solved the issue like this:
-(void)clickOnSend:(id)sender{
NSInteger dynamicRows = [self.createUserTableView numberOfRowsInSection:0];
for (int i=0; i<dynamicRows; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UserProfileCell *cell = (UserProfileCell *)[self.createUserTableView cellForRowAtIndexPath:indexPath];
DLog(#"cell textfield string :%#",cell.valueTextField.text);
}
}
Related
I have one table view cell with one button. On click of the cell button which has one method to check the if button tag equals to indexPath.row. Then I am trying to change the image. But its not showing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"FilterTableViewCell";
FilterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.cellBtn.tag = indexPath.row;
cell.radioImage.tag = indexPath.row;
[cell.cellBtn addTarget:self action:#selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)ButtonClicked:(UIButton*)sender
{
int rowNum = [(UIButton*)sender tag];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:_currentSelectedIndex inSection:0];
FilterTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
if (sender.tag == rowNum) {
cell.radioImage.image = [UIImage imageNamed:#"selected.png"];
} else {
cell.radioImage.image = [UIImage imageNamed:#"unselected.png"];
}
}
Here button image is not changing. What I need is, when ever I click on any cell that particular cell image needs to change and other cells should show unselected image. And when ever I click on any cell which image is changed as selected image. I needs to get that particular cell values like textlabel name, subtextlabel name.
How can i achive that. What's the problem here ?
Updated :
-(void)ButtonClicked:(UIButton*)sender
{
int rowNum = [(UIButton*)sender tag];
FilterTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowNum inSection:0]];
if (sender.tag != _currentSelectedIndex) {
cell.radioImage.image = [UIImage imageNamed:#"selected.png"];
} else {
cell.radioImage.image = [UIImage imageNamed:#"unselected.png"];
}
}
add these two delegates of tableView:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
FilterTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
cell.radioImage.image = [UIImage imageNamed:#"unselected.png"];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FilterTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
cell.radioImage.image = [UIImage imageNamed:#"selected.png"];
}
and then change ButtonClicked to:
-(void)ButtonClicked:(UIButton*)sender
{
int rowNum = [(UIButton*)sender tag];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:_currentSelectedIndex inSection:0];
if (sender.tag == rowNum) {
[_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}
I read your code. What i found is that you made a mistake in the button tapped method.
-(void)ButtonClicked:(UIButton*)sender
{
int rowNum = [(UIButton*)sender tag];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:_currentSelectedIndex inSection:0];
FilterTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
if (sender.tag == rowNum) {
cell.radioImage.image = [UIImage imageNamed:#"selected.png"];
} else {
cell.radioImage.image = [UIImage imageNamed:#"unselected.png"];
}
}
The if condition which you have placed is always return true because first you store the sender's tag to rowNum and then you compare both the two.
CONDITION ALWAYS BE TRUE AND FOLLOWING CODE WILL BE EXECUTED
cell.radioImage.image = [UIImage imageNamed:#"selected.png"];
I have a table with number of cells, with each cell having one UIButton. When I click on that button the button cell background color should change. Here is my partial code for your understanding.
When I click on this button all my tableview cells background colors are getting changed. How to do it only for the selected cell?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableCell";
cell = (MenuCell *)[menuTableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell initState];
// Listens for method calls on cell
cell.delegate = self;
}
if ((self.TempArray.count != 0)) {
for (int i=0; i<TempArray.count; i++) {
if ([self.TableviewArray objectAtIndex:indexPath.row] == TempArray[i]) {
if (cell.backgroundColor != [UIColor lightGrayColor]) {
cell.backgroundColor = [UIColor lightGrayColor];
}
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=indexPath.row;
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"UnHide" forState:UIControlStateNormal];
button.frame = CGRectMake(200.0, 20.0, 160.0, 40.0);
[cell.contentView addSubview:button];
}
}
}
// Set index for this cell (it could be wrong if cell is re-used)
cell.downloadIndex = (int)indexPath.row;
Download *download = [TableviewArray objectAtIndex:indexPath.row];
cell.downloading = download.downloading;
cell.completed = download.completed;
cell.MenuLang.text = download.name;
cell.downLoadPrgView.progress = download.percentageDownloaded;
// Check for completed status
cell.completed = download.completed;
cell.CompletedMenuLang.text = download.name;
return cell;
}
-(void)aMethod:(UIButton*)sender {
NSLog(#"I Clicked a button %d",sender.tag);
int row = sender.tag;
}
Just implement below code, you can change background colour of particular cell as well as hide the selected button.
-(void)aMethod:(UIButton*)sender {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
Friends1TableViewCell *cell = (Friends1TableViewCell *)[self.tblVW cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
sender.hidden = YES;
}
-(IBAction)BTNTapped:(UIButton *)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero
toView: menuTableview];
NSIndexPath *tappedIP = [menuTableview indexPathForRowAtPoint:buttonPosition];
}
do change using this indexpath of cell
Here is my answer..
-(void)aMethod:(UIButton*)sender
{
NSLog(#"I Clicked a button %d",sender.tag);
int row = sender.tag;
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *cell = [menuTableview cellForRowAtIndexPath:path];
cell.contentView.backgroundColor = [UIColor whiteColor];
}
try this
#property (nonatomic, assign) NSInteger selectedRow;
self.selectedRow = -1; // init with -1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
if(self.selectedRow == indexPath.row) {
cell.backgroundColor = SELECTED_COLOR;
}
else {
cell.backgroundColor = NORMAL_COLOR;
}
return cell;
}
-(void)aMethod:(UIButton*)sender {
// ...
self.selectedRow = row;
[self.myTableView reloadData];
}
My Custom tableview cell content getting empty after scrolling.So pls help me with this.
My custom cell has 8 buttons and a label.First I'm showing only label that has title and on selection I'm expanding the cell and showing all buttons.So, when I select few buttons and I do scrolling,buttons that I selected getting refreshed or get back to normal state.Here is the code.Pls help me with this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"newFilterCell";
newFilterCell *cell = (newFilterCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"newFilterCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell.contentView.superview setClipsToBounds:YES];
}
cell.QuestionLabel.text=[orderArray objectAtIndex:indexPath.row];
NSArray * arr = [filterInfo objectForKey:[orderArray objectAtIndex:indexPath.row]];
int val = 0;
NSLog(#"%#",cell.subviews);
NSArray * cellViews = cell.contentView.subviews;
if (arr.count>0)
{
for (int i=1; i<=8; i++) {
if (i<=arr.count) {
UIButton * target = (UIButton*)[cell viewWithTag:i];;
[target setTitle:[arr objectAtIndex:i-1]forState:UIControlStateNormal];
[target addTarget:self action:#selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
[[cell viewWithTag:i] setHidden:YES];
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
On selection of my cell I'm expanding and reloading the tableview cells.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath==_expandIndexPath) {
_expandIndexPath = nil;
NSMutableArray *modifiedRows = [NSMutableArray array];
[tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationLeft];
}
else
{
selectedRow=indexPath.row;
NSMutableArray *modifiedRows = [NSMutableArray array];
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
_expandIndexPath = indexPath;
[modifiedRows addObject:indexPath];
[tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationLeft];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray * arr = [filterInfo objectForKey:[orderArray objectAtIndex:indexPath.row]];
if ([indexPath isEqual:_expandIndexPath])
{
if ([[orderArray objectAtIndex:indexPath.row]isEqualToString:#"Height"]||[[orderArray objectAtIndex:indexPath.row]isEqualToString:#"Age"])
{
return 275;
}
else
{
if(arr.count==3)
return 55*arr.count;
else
return 37*arr.count;
}
}
else
return 70;
}
UITableViewCells are getting recycled. That's why its not safe to do it your way. Your data model needs to remember your buttons and other stuff that changed. You need to apply the changes every time the cell gets created.
You need to check in the cellForRowAtIndexPath what button is pressed and then show the view correctly.
You need to remember what happend in the cells with an external data source to apply the changes you want.
In your cellForRowAtIndexPath should be something like a check for a boolean whether or not you show some stuff:
if(button_is_pressed_for_row_5 == true){
button_in_cell.hidden = true;
}else{
button_in_cell.hidden = true;
}
I am loading a UIWebview in a expand/collapse UITableView. I need to change the row height according to the UIWebview content height. if i set the UIWebview delegate in the UITableview, the UIWebview delgate method keeps get calling over and over. i am calling the webviewDidFinishLoad method to get the content height. Any kind of help/suggestion would be very appreciative.
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
return YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.restauRantMenuArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self tableView:self.restaurantMenuTableView canCollapseSection:section])
{
if ([expandedSections containsIndex:section])
{
return 2; // return rows when expanded
}
return 1; // only top row showing
}
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:self.restaurantMenuTableView canCollapseSection:indexPath.section])
{
if (indexPath.row == 0)
{
static NSString *MyIdentifier = #"menuTitleCell";
RestaurantMenuTitleTableViewCell *cell =(RestaurantMenuTitleTableViewCell*) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[RestaurantMenuTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RestaurantMenuTitleTableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
RestaurantMenuItemObject *object = [self.restauRantMenuArray objectAtIndex:indexPath.section];
cell.titleWebView.opaque = NO;
cell.titleWebView.backgroundColor = [UIColor clearColor];
[cell.titleWebView loadHTMLString:object.menuTitle baseURL:nil];
cell.titleWebView.scrollView.scrollEnabled = NO;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
else
{
static NSString *MyIdentifier = #"menuContentCell";
RestaurantMenuContentTableViewCell *cell =(RestaurantMenuContentTableViewCell*) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[RestaurantMenuContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RestaurantMenuContentTableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
RestaurantMenuItemObject *object = [self.restauRantMenuArray objectAtIndex:indexPath.section];
cell.contentWebView.tag = indexPath.section + 1000;
cell.contentWebView.opaque = NO;
cell.contentWebView.backgroundColor = [UIColor clearColor];
[cell.contentWebView loadHTMLString:object.menuContent baseURL:nil];
//cell.contentWebView.scrollView.delegate = self;
[cell.contentWebView.scrollView setShowsHorizontalScrollIndicator:NO];
// cell.contentWebView.scrollView.scrollEnabled = NO;
//cell.contentWebView.delegate = self;
if([[self.imageHeightArray objectAtIndex:indexPath.section] isEqualToString:#"150"])
{
//cell.contentWebView.delegate = self;
}
// first row
//cell.textLabel.text = #"Expandable"; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
//cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
//cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown]
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
return cell;
}
}
else
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor clearColor];
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RestaurantMenuItemObject *object = [self.restauRantMenuArray objectAtIndex:indexPath.section];
UIWebView *thisWebView = (UIWebView*)[self.restaurantMenuTableView viewWithTag:indexPath.section+1000];
[thisWebView loadHTMLString:object.menuContent baseURL:nil];
thisWebView.delegate = self;
//[tableView deselectRowAtIndexPath:indexPath animated:YES];
//if there is any previous selected index
//if selected index is not the previous selected
if (selectedIndex && (selectedIndex.section != indexPath.section)) {
[self collapseOrExpandForIndexPath:selectedIndex inTableView:tableView];
}
if ([selectedIndex isEqual:indexPath]) {
selectedIndex = nil;
}
else{
selectedIndex = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
}
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
[self collapseOrExpandForIndexPath:indexPath inTableView:tableView];
if (indexPath.row == 0) {
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
}
- (void)collapseOrExpandForIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView{
if (!indexPath.row)
{
// only first row toggles exapand/collapse
//[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
else
{
//RestaurantMenuItemObject *object = [self.restauRantMenuArray objectAtIndex:indexPath.section];
// UIWebView *thisWebView = (UIWebView*)[self.restaurantMenuTableView viewWithTag:indexPath.section+1000];
//
// [thisWebView loadHTMLString:object.menuContent baseURL:nil];
//
// thisWebView.delegate = self;
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//return UITableViewAutomaticDimension;
if(indexPath.row == 0)
return 50.00;
else
{
return 240;//[[self.imageHeightArray objectAtIndex:indexPath.section] integerValue];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 1.00;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1.00;
}
pragma mark Table View Delegate Method ends
Create a class level property (CGFloat),say webViewHeight to track web view's height.
On table view didSelectRowAtIndexPath method load url in web view.
Calculate web view height in webviewDidFinishLoad delegate method of web view and set it in class level height variable,webViewHeight. After calculating height call [tableView reloadData];
Set selected cell height with webViewHeight and rest with default,say 44.0 in heightForRowAtIndexPath method of data source.
IMP: Set maximum web view height also if calculated height exceed max height set calculated height to max height. And enable web view scrolling so that user is able to view all web view content.
I'm using a custom table view cell with a stepper and a label.
But when i check the value of the stepper it increases only once for every 2 presses.
What is the problem?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cellData";
ItemCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[ItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.stepper.tag = indexPath.row;
cell.countLabel.text = [NSString stringWithFormat:#"%g",cell.stepper.value];
NSLog(#"value = %g",cell.stepper.value);
cell.itemLabel.text = [self.groceryItems objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)valueChanged:(id)sender
{
UIStepper *step = sender;
NSIndexPath *index = [NSIndexPath indexPathForRow:step.tag inSection:0];
NSArray *rowsToReload = [NSArray arrayWithObject:index];
[self.myTableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:NO];
}
try to add this to your valueChanged Method:
[self.myTableView beginUpdates];
[self.myTableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:NO];
[self.myTableView endUpdates];