I have a custom tableview cell which has a button. I need to pass the NSIndexPath object of tablview when the button is clicked. I am able to do is assign a tag to a button, receive the tag using sender..but I want to pass the NSIndexPath object...below is the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"shortCodeCell";
IPadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"KeywordsCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[IPadTableViewCell class]])
{
cell = (IPadTableViewCell *)currentObject;
}
}
}
// Delete
[cell.btnDelete addTarget:self action:#selector(onDelete:) forControlEvents:UIControlEventTouchUpInside];
cell.btnDelete.tag=indexPath.row;
return cell;
}
-(void)onDelete:(id)sender
{
UIButton *btn=(UIButton *)sender;
NSLog(#"BtnIndexpath to be deleted:%d",btn.tag);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
int errorCode = 0;
kd = [items objectAtIndex:btn.tag];
BOOL isDeleteKeyword= [ServerAPI deleteKeywordWithId:kd.keywordId :&errorCode];
dispatch_async (dispatch_get_main_queue (),
^{
if (isDeleteKeyword) {
[items removeObjectAtIndex:btn.tag];
//[tblKeywords deleteRowsAtIndexPaths:[NSArray arrayWithObject:btnIndexPath] withRowAnimation:YES];
[self.tblKeywords reloadData];
}
else return;
});
});
}
You can set the following in "cellForRowAtIndexPath"
cell.tag = indexPath.section;
cell.btnDelete.tag=indexPath.row;
And in "onDelete" you can create indexPath based on tags assigned.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:[[sender superview] tag]]
// I assume your UITableView called tableView
// Using this way you don't need to use tag
-(void)onDelete:(id)sender
{
UITableViewCell *cell = (UITableViewCell *)[sender superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
Related
I have to change button title color and image of only one cell which is selected. suppose I have clicked side bar button from main view controller then in side bar, only home buttton should be in blue color and others should be in black. I have attached screen shot to show an example.
I have written my code as below
- (IBAction)home:(id)sender {
titleSelected = [NSUserDefaults standardUserDefaults];
[titleSelected setObject:#"home" forKey:#"CellIdentifier"];
[titleSelected synchronize];
NSLog(#"defaultValue %#",[titleSelected stringForKey:#"CellIdentifier"]);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ if (indexPath.row==0) {
static NSString *simpleTableIdentifier = #"but1";
_oneCell= (oneTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (_oneCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"oneTableViewCell" owner:self options:nil];
_oneCell = [nib objectAtIndex:0];
}
_oneCell.titleImage.layer.cornerRadius = _oneCell.titleImage.frame.size.width / 2;
_oneCell.titleImage.clipsToBounds = YES;
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"Imagekey"] != nil) {
NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:#"Imagekey"];
_oneCell.titleImage.image = [UIImage imageWithData:imageData];
} else {
_oneCell.titleImage.image = [UIImage imageNamed:#"user.png"];
}
return _oneCell;
}
if (indexPath.row==1) {
static NSString *simpleTableIdentifier = #"but2";
_oneCell= (oneTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if ([[titleSelected stringForKey:#"CellIdentifier"] isEqualToString:#"home"]) {
NSLog(#"defaultValue %#",[titleSelected stringForKey:#"CellIdentifier"]);
[_oneCell.homeButtonpressed setTitleColor:[UIColor colorWithRed:(5/255.0) green:(103/255.0) blue:(169/255.0) alpha:1.0] forState:UIControlStateNormal];
}
return _oneCell;
}
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
but it is not working. Please Help me.
try this line of code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //Get your cell for selected row
cell.leftMenuItemLabel.textColor = [UIColor redColor];//Configure whatever color you want
}
I have a button on cell On click of this button I want to open a view, which work fine. Now problem is when we click on next cell button, my previous view doesn't hides.
Here is what I am trying two way but not work. Please help .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CustumCellTableViewCell";
CustumCellTableViewCell *cell = (CustumCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustumCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.touchButton.tag =indexPath.row;
[cell.touchButton addTarget:self action:#selector(menuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
cell.menuView.hidden = YES;
return cell;
}
- (void) menuButtonClick:(id) sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
CustumCellTableViewCell *cell=(CustumCellTableViewCell *)[table cellForRowAtIndexPath: indexPath];
if (cell.menuView.hidden) {
cell.menuView.hidden = NO;
} else {
cell.menuView.hidden = YES;
}
1 method
for ( CustumCellTableViewCell *cell1 in table.visibleCells ) {
NSLog(#"%#",cell1);
if (cell1 != cell) {
cell.menuView.hidden = YES;
}
}
2 method
for (NSIndexPath *indexpaths in [table indexPathsForVisibleRows] ) {
if (indexpaths == indexPath) {
cell.menuView.hidden = NO;
}
else
{
cell.menuView.hidden = YES;
}
}
}
You are using currently selected cell. If you want to hide last selected cell menu than you have to store last selected cell index.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastSelectedCell inSection:0];
CustumCellTableViewCell *cell=(CustumCellTableViewCell *)[table cellForRowAtIndexPath: indexPath];
cell.menuView.hidden = YES;
/*
your code goes here
*/
lastSelectedCell = [sender tag];
[for ( CustumCellTableViewCell *cell1 in \[table visibleCells\] ) {
if (cell1 != cell) {
cell1.menuView.hidden = YES;
}
else
{
cell1.menuView.hidden = NO;
}
}][1]
[1]: http://
you can add cell tag with indexPath.row and compare cell.tag is equal to [sender tag] or not. Also check your sender tag value is correct or not.
- (void) menuButtonClick:(id) sender
{
[table reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
CustumCellTableViewCell *cell=(CustumCellTableViewCell *)[table cellForRowAtIndexPath: indexPath];
if(cell.tag==[sender tag]){
cell.menuView.hidden = NO;
} else {
cell.menuView.hidden = YES;
}
}
Iam using the did select method to select the data .so data in a particular row is selecting and aim getting the check mark also now my problem is when the check mark is on state the data should be printed in console and if its off the data should be removed from the array. my code is as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(#"array is %#",array);
static NSString *CustomCellID = #"cell";
contactcellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellID];
if (cell == nil)
{
cell=[[contactcellTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellID];
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"contactcellTableViewCell" owner:self options:nil];
cell =[nib objectAtIndex:0];
}
cell.Firstnamelbl.text = [[array objectAtIndex:indexPath.row] objectForKey:#"first_name"];
cell.Lastnamelbl.text=[[array objectAtIndex:indexPath.row]objectForKey:#"last_name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[array removeAllObjects];
contactcellTableViewCell *cell = (contactcellTableViewCell *) [tableView cellForRowAtIndexPath:indexPath];
static NSString *CustomCellID = #"cell";
if (cell == nil)
{
cell=[[contactcellTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellID];
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"contactcellTableViewCell" owner:self options:nil];
cell =[nib objectAtIndex:0];
}
if (cell.m_checkImageView.image == [UIImage imageNamed:#"Selected.png"])
cell.m_checkImageView.image = [UIImage imageNamed:#"Unselected.png"];
else
cell.m_checkImageView.image = [UIImage imageNamed:#"Selected.png"];
}
I had two labels in cell so that when the particular row is selected the data should be print on the console
i got the answer please find below
if (cell.m_checkImageView.image == [UIImage imageNamed:#"Selected.png"])
{
cell.m_checkImageView.image = [UIImage imageNamed:#"Unselected.png"];
}
else
{
cell.m_checkImageView.image = [UIImage imageNamed:#"Selected.png"];
NSLog(#"string is %#",string);
}
}
now we will see the data when row is selected
I did a UITableView filled with a plist data source, and I did custom cells with Interface Builder (so I'm using xib files)
here's the part of code where I'm creating cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"DataTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if ([view isKindOfClass:[UITableViewCell class]]) {
cell = (DataTableViewCell*)view;
}
}
}
return cell;
}
then when I use the:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method to get the selected cell, I call this method to expand the cell:
- (void)expandCellAtIndex:(int)index {
NSMutableArray *path = [NSMutableArray new];
NSArray *currentCells = [subCellItems objectAtIndex:index];
int insertPos = index + 1;
for (int i = 0; i < [currentCells count]; i++) {
[path addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
Now the problem is that since I've not done this before, I'm stuck on the logic on how to change the cell I want to show when expanded, that's because the method:
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
is searching for the right path and inserting a cell with the same cell xib file as the one I've loaded with my data at the start
how can I set a different cell (with a different xib, and different data) to show instead of the same as before?
basically I have this table working but in the expanding cell I see a copy of the cell you touch
You can do it like this:
In .h:
#interface TSViewController : UIViewController
{
NSMutableArray* subCellIndexPaths;
}
In DataTableViewSubCell.xib create one "Table View Cell" (other Views you have to remove). Change Class for "Custom Class" to DataTableViewSubCell. Replay it for second cell.
In .m:
- (UITableViewCell*) tableView: (UITableView*) tableView
cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
static NSString *CellIdentifier = #"Cell";
DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
if ([subCellIndexPaths containsObject: indexPath])
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed: #"DataTableViewSuperCell"
owner: nil
options: nil];
cell = [nib objectAtIndex: 0];
}
else
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed: #"DataTableViewSubCell"
owner: nil
options: nil];
cell = [nib objectAtIndex: 0];
}
}
return cell;
}
- (void) expandCellAtIndex: (int)index
{
NSMutableArray* indexPaths = [NSMutableArray new];
NSArray* currentCells = [subCellItems objectAtIndex: index];
int insertPos = index + 1;
for (int i = 0; i < [currentCells count]; i++)
{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow: insertPos++
inSection: 0];
[indexPaths addObject: indexPath];
[subCellIndexPaths addObject: indexPath];
}
[self.tableView insertRowsAtIndexPaths: indexPaths
withRowAnimation: UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: index
inSection: 0]
atScrollPosition: UITableViewScrollPositionTop
animated: YES];
}
And of course you must create subCellIndexPaths anywhere (in init or viewDodLoad methods).
I know similar questions have been asked few times about how to add badge to tableviewcell, but I could not make it working
Basically what I want is to show user a simple notification either a red number at the right part of the table view cell or a rectangle or like native email app.
So I have tried both of this two source code TDbadgcell and DDbadgecell
Now the problem is I can not delegate them, I have to import their .h classes and call either one of the below functions in my table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TDBadgedCell *cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
or
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DDBadgeViewCell *cell = (DDBadgeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DDBadgeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
But when I do that my tableView didSelectRowAtIndexPath: and (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender methods are not working, I can click the rows but they stay higlihted blue nothing happens also my arrow at the right side of the table is dissappears.
So how can I achieve to add a badge to table view cell row either with above source codes or any other methods?
EDIT:::
After putting NSLOG I can see that did select row is called but perform segue still does not work. Without adding any of the above code it works perfect.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = #"MeetingCell";
static NSString *CellIdentifier = #"Cell";
DDBadgeViewCell *cell = (DDBadgeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DDBadgeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *description =#":";
NSString *name =#"";
NSString *fileStatus=#"";
name = [[self agenda] getFileNameWithSection:[indexPath section] Row:[indexPath row]];
description = [[self agenda] getFileDescriptionWithSection:[indexPath section] Row:[indexPath row]];
fileStatus = [[self agenda] getFileStatusWithFileName:name];
NSString * cellLabel = [NSString stringWithFormat:#" %# : %#",description,name];
//alloc row images
UIImage *docImage = [UIImage imageNamed:#"ICON - Word#2x.png"];
UIImage *xlsImage = [UIImage imageNamed:#"ICON - Excel#2x.png"];
// UIImage *picImage = [UIImage imageNamed:#"ICON - Image#2x.png"];
UIImage *pdfImage = [UIImage imageNamed:#"pdf icon#2x copy.png"];
UIImage *pptImage = [UIImage imageNamed:#"ICON - PPT#2x.png"];
//Determine what status to display for a file
//No need to that since wee use push notification
if ([fileStatus isEqualToString:#"new"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"New"];
cell.badgeText = [NSString stringWithFormat:#"Update"];
cell.badgeColor = [UIColor orangeColor];
}else if ([fileStatus isEqualToString:#"outdated"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"Outdated"];
cell.badgeText = [NSString stringWithFormat:#"Update"];
cell.badgeColor = [UIColor orangeColor];
}else if ([fileStatus isEqualToString:#"updated"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"Latest"];
}
UIFont *font1 = [UIFont fontWithName:#"Century Gothic" size:15.0f];
cell.textLabel.font=font1;
//if there is no file user can not tocuh the row
if ([name length]==0) {
cell.userInteractionEnabled = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = description;
}else{
//set cell title
cell.textLabel.text = cellLabel;
}
//set row images
if ([name rangeOfString:#"docx"].location != NSNotFound) {
cell.imageView.image= docImage;
}else if ([name rangeOfString:#"xlsx"].location != NSNotFound){
cell.imageView.image= xlsImage;
}
else if ([name rangeOfString:#"pdf"].location != NSNotFound){
cell.imageView.image= pdfImage;
}
else if ([name rangeOfString:#"ppt"].location != NSNotFound){
cell.imageView.image= pptImage;
}
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rounded corner box center#2x.png"]];
// At end of function, right before return cell
cell.textLabel.backgroundColor = [UIColor clearColor];
NSLog(#"%#",cell.textLabel.text);
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];.0
*/
NSLog(#"didselect row is called %d",indexPath.row);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[DBSession sharedSession] isLinked]) {
if([[segue identifier] isEqualToString:#"pushDocumentView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSInteger section =[indexPath section];
NSInteger row = [indexPath row];
NSString *fileName = [[self agenda] getFileNameWithSection:section Row:row];
NSDictionary * agendaDic = [[[self agenda]revision] objectForKey:fileName];
NSString *fileStatus =[agendaDic objectForKey:#"status"];
DocumentViewController *docViewController = [segue destinationViewController];
//This will display on the Document Viewer
docViewController.fileName=fileName;
//This will determine remote or local copy display
docViewController.fileStatus=fileStatus;
}
}else {
[self displayError];
[self setWorking:NO];
}
}
Just called a perfromseguewith Identifier
because this is called - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender also when you call [self performSegueWithIdentifier:#"yoursegue" sender:self];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];.0
*/
[self performSegueWithIdentifier:#"yoursegue" sender:self];
NSLog(#"didselect row is called %d",indexPath.row);
}