-5

I am displaying a button in cutstom cell in UITableview. How do I hide that button when it is not needed. For example: I am display received images count on button. In case count will be zero, I need to hide that button from cell.

contactviewController.m
if (![[arr objectAtIndex:4] isEqualToString:@"0"]) {
            [cell1 setImg:[arr objectAtIndex:4]];
        }
Customcell.m

-(void)setImg:(NSString *)_text
{

        imgView.titleLabel.textColor = [UIColor whiteColor];
        [imgView setTitle:_text forState:UIControlStateNormal];
    }
user2003416
  • 159
  • 2
  • 9

4 Answers4

0

You can do this in your cellForRowAtIndex method. For each row creation this method is called.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(condition)
    {
        //show button
    }
    else
    {
        // don't show button
    }
}
Rushi
  • 4,553
  • 4
  • 33
  • 46
  • @user2003416 : Instead of doing the action in customcell.m put this condition in the class where you are using the customcell. – Rushi Feb 04 '13 at 06:42
0
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 {
      if(noImagesFound)
      {
           yourCustomCellObject.buttonObject.hidden = YES;
      }
      else
      {
           yourCustomCellObject.buttonObject.hidden = NO;
      }
 }
Hemang
  • 26,840
  • 19
  • 119
  • 186
0

You can add buttons in cell via xib as IBOutlets or programmatically by setting frame and adding the button to the cell. The solution for above depends on how you have added the button. If you added it via xib as an outlet, you can either do one of the following.

There a property to any view called tag. You can set the tag value in the attribute inspector for the button and in the -cellForRowAtIndexPath: call as

UIButton *button  =(UIButton*) [cell viewWithTag:9];

// your tag value (say 9)

and the use [button setHidden:YES] to hide //NO to unhide here you dont need a custom class

  1. If you have created custom class for the same ,where you can add the button via two methods either by programmatically or via xib. If its xib, create a IBOutlet and make it a property of the custom cell so that you can get access to it using the custom cell object.

It's better to opt the second option if you need more control over the cell you created. If you had made it a property you can get access to it by using the object of the cell.

DocMax
  • 12,094
  • 7
  • 44
  • 44
Meera
  • 1,031
  • 6
  • 25
  • i created object for custom cell class and used it to hide button in cellForRowAt method.it is not hiding – user2003416 Feb 04 '13 at 07:01
  • How did you connected the button to the custom cell? Check the connection you did! – Meera Feb 04 '13 at 07:10
  • dont' you have a @property outlet for that button in that custom cell? @ – DD_ Feb 04 '13 at 07:11
  • viewcontroller.m:contactObj.imgView.hidden=YES; – user2003416 Feb 04 '13 at 07:16
  • viewcontroller.h:ContactPicsCustom *contactObj; – user2003416 Feb 04 '13 at 07:16
  • contactcustom.h:@property (nonatomic, retain) IBOutlet UIButton *imgView; – user2003416 Feb 04 '13 at 07:17
  • dude, add that button in your customcell class .h itself (@ property outlet ) and when you create its object in your viewcontroller.m, you can set it hidden by calling, for eg; cellClass.imageButton..and so – DD_ Feb 04 '13 at 07:19
  • @user2003416 have you connected this to the button in xib? are you able to set text to it? – Meera Feb 04 '13 at 07:19
  • @MilKyWaY He might have set setHidden to NO at some point later after hiding it. – Meera Feb 04 '13 at 07:24
  • @MeeraJPai -(void)setImg:(NSString *)_text method is written in customcell!! How will this work yar! – DD_ Feb 04 '13 at 07:26
  • @MilKyWaY or he can do this way. If the setting of text is working properly, unhide the button and then set the value in that method and if the string is empty,hide it. Hope this works. I can't provide exact solution unless I get to know the working of his code. – Meera Feb 04 '13 at 07:31
  • @MeeraJPai he doesn't seem to wait for a solution.So I quit. – DD_ Feb 04 '13 at 07:39
-2
  1. Create UITableViewCell.h and .m file.

  2. Create some variable like UILabel and UIImageView object in files and make it IBOutlet and bind them with cell .xib file.

  3. Inside UITableView implementation, and in "cellForRowAtIndexPath" you can use that custom UITableViewCell class object and use that synthesize variable of UILable and UIImageView and use it to show or hide accordingly.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Nishant B
  • 2,897
  • 1
  • 18
  • 25