how to hide custom UIButton in uitableviewcell? - iphone

I am using a custom UItablecell for rows in one of my uitableviews. However, the customtabelcell has a UIButton that I would like to hide in the UItableview display. The custom tablecell was designed in IB. I tried the following code but it doesn't seem to hide the uibutton:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *cellIdentifier = #"customTableCell1";
customTableCell1* myOrderCell1 = (customTableCell1*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (myOrderCell1 == nil)
{
NSLog(#"Cell created");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"customTableCell1"
owner:nil
options:nil];
for(id currentObj in nibObjects)
{
if ([currentObj isKindOfClass:[customTableCell1 class]] )
{
myOrderCell1 = (customTableCell1 *)currentObj;
}
}
}
myOrderCell1.backgroundColor = [UIColor blackColor];
myOrderCell1.accessoryType = UITableViewCellAccessoryNone;
**myOrderCell1.AddToCartBtn.hidden = YES;
myOrderCell1.AddToCartBtn.enabled = NO**;
return myOrderCell1;
}
Does anyone have some idea as to what to do?
Thanks

Are you sure you set the button outlet in IB?

Related

iPhone UITableViewCell SwipeGesture

I wanted to simply animate custom view in a custom table cell by swiping left or right in that cell. I am loading the cell from the xib.
This is the code I used to populate the cell from the xib.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
CustomCell *CustomCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (CustomCell == nil)
{
NSArray *cells =[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
for (UIView *view in cells)
{
if([view isKindOfClass:[UITableViewCell class]])
{
CustomCell = (CustomCell *)view;
break;
}
}
}
CustomCell.textLabel.Text = #"this is a text";
return CustomCell;
}
What I need is to swipe a cell to reveal a background view with buttons. I have searched and find many tutorials regarding this but none of the same is using the custom cell with xib.
Check out this project
UITableViewCell Swipe
hi try this for your requirement of swipe a cell to reveal a background view with buttons.
jzswipecell

Duplicate UItableViewCell, Rows of UITableView

Here tableview after scrolling 2 3 times full up and down add images to all cells. Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:#"HomeCell"];
if (cell == nil) {
cell = [[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"HomeCell"] ;
}
VenueDC *venueObj = [subSubCategoriesArray objectAtIndex:indexPath.row];
cell.lblName.text = venueObj.name;
if ([venueObj.imagesArray count] > 0) {
[cell.ivVenue setImage:venueObj.ivVenue];
[cell.ivVenue setHidden:NO];
cell.lblName.frame = CGRectMake(80, cell.lblName.frame.origin.y, 200, cell.lblName.frame.size.height);
}
venueObj = nil;
return cell;
}
Any idea what's happening here?
Image is only in one object, on first load it shows one cell with image, but after scrolling it starts showing images on other cell too.
Solved it like this, first i set the lblName frame to its original position, so it reset its position everytime the cell is used
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:#"HomeCell"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"HomeCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (HomeCell *) currentObject;
break;
}
}
}
VenueDC *venueObj = nil;
venueObj = [subSubCategoriesArray objectAtIndex:indexPath.row];
cell.lblName.text = venueObj.name;
[cell.ivVenue setHidden:YES];
cell.lblName.frame = CGRectMake(20 , 19, 250, 20);
if (venueObj.ivVenue) {
[cell.ivVenue setImage:venueObj.ivVenue];
[cell.ivVenue setHidden:NO];
cell.lblName.frame = CGRectMake(80, cell.lblName.frame.origin.y, 200, cell.lblName.frame.size.height);
}
return cell;
}
UITableViewCells get reused, which is what the dequeueReusableCellWithIdentifier: method does. To save memory the UITableView only instantiates as many cells as it is possible to see at any given point, and then just keeps on reusing them. Because of this you should always clean up your cells before reuse, that is reset all its content to nil.
If you are using a custom table view like in your case HomeCell then override -(void)prepareForReuse and set up the image to nil there.
It is happening because your cell is reuse so write this code:
HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"HomeCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (HomeCell *) currentObject;
break;
}
}
}
You Need to add only one line in cellForRowAtIndexPath
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
And change
HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:#"HomeCell"];
if (cell == nil) {
cell = [[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"HomeCell"] ;
}
VenueDC *venueObj = [subSubCategoriesArray objectAtIndex:indexPath.row];
cell.lblName.text = venueObj.name;
[cell.ivVenue setImage:nil];
if ([venueObj.imagesArray count] > 0) {
[cell.ivVenue setImage:venueObj.ivVenue];
[cell.ivVenue setHidden:NO];
cell.lblName.frame = CGRectMake(80, cell.lblName.frame.origin.y, 200, cell.lblName.frame.size.height);
}
venueObj = nil;
return cell;
}
You need an else statement:
else {
venueObj = nil;
}

Custom Cell UITableview

I want to have a custom cell in my UITableview. I have tried it with the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell*)view;
}
}
}
[cell setLabelText:[daten objectAtIndex:indexPath.row]];
return cell;
}
But I am getting an exception on that line:
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
I'm almost sure that you have lost connection in your XIB file. If commentLabel is still exists you should either delete it and create a new outlet or check the outlets section and delete extra connection and all will be alright!!
EDIT:
This error occurs when you have two outlets connected to the same label in IB.
This is how I made my custom cell and called it from the class :-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[cell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = customCell;
customCell = nil;
}
// Configure the cell.
cell.serialLabel.text = [[NSNumber numberWithInt:indexPath.row+1]stringValue];
cell.textLabel.textColor=[UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
if u are not using the latest ios SDK, make sure that u have written
#synthesize commentLabel;
in CustomCell
Try cleaning the build folder, or try Simulator -> Reset Content and Settings.

iPhone - UITableViewCell having UIView which has 2 UIButton

I've a table view. In each cell (row), i want to show two buttons. initially both are red in color. when one button is clicked, it turns green and other will be in red.
I created a view which has two buttons. I am using IB to create the view.
i'm using following code to show the my table view to show the custom view:
- (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:CellIdentifier] autorelease];
}
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:#"CellView"
owner:self options:nil];
CellView *myView;
for (id object in bundle) {
if ([object isKindOfClass:[CellView class]])
myView = (CellView *)object;
}
NSString* str = [testArray objectAtIndex:indexPath.row];
NSArray* arr = [str componentsSeparatedByString:#" "];
myView.left.titleLabel.text = [arr objectAtIndex:0];
myView.right.titleLabel.text = [arr objectAtIndex:1];
[cell.contentView addSubview:myView];
return cell;
}
Above code works fine but when the button is clicked, its showing the button with text that I created in IB. I am not understanding why is it happening?
Can some one guide me how to show a button in cell and handle its action?
I think you have to specify the titleLabel for different states of button. Then this code will work fine.

Add custom action:#selector to UISwitch on TableViewCell

I have TableCellViewController for managing cells in my UITableView. Each cell has a label na UISwitch (dictTypeSwitch). A want to assign method to switch events so I can save the state of button.
So far I've done this:
assign setState function to object:
[cell.dictTypeSwitch addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchUpInside];
function for handling event:
- (void)setState:(id)sender {
BOOL state = [sender isOn];
NSString *rez = state == YES ? #"YES" : #"NO";
NSLog(rez);
}
From sender I get UISwitch object from which I can get state.
So far everything is ok.
But if I want to save the state of UISwitch, I have to get rowIndex for this cell.
How can I achieve this?
The cells are made inside function cellForRowAtIndexPath. Se the code bellow:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"DictionariesTableCellViewController";
DictionariesTableCellViewController *cell = (DictionariesTableCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DictionariesTableCellViewController" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (DictionariesTableCellViewController *) currentObject;
break;
}
}
}
// Configure the cell...
cell.dictTypeLabel.text = [NSString stringWithFormat:#"row - %i",indexPath.row];
[cell.dictTypeSwitch addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Thanks
You can use the UISwitch's .tag property to store an arbitrary integer. This can be set to the row index.
If it is possible to find the UITableViewCell from UISwitch's parent, you could get the index path with
NSIndexPath* indexPath = [theTableView indexPathForCell:theCell];