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
Related
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.
In my app I have custom UITableViewCell, and I have UIStepper and UILabel in the custom cell. I don't know how to check which stepper was clicked. So is it a way to know from which cell stepper was clicked?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
if ([nib count] > 0) {
cell = self.tbcell;
}
}
return cell;
}
An alternative is:
In the method that gets fired when the UIStepper value changes (e.g. #max_, above), you can do the following:
- (IBAction)stepperValueDidChanged:(UIStepper *)sender {
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
// assuming your view controller is a subclass of UITableViewController, for example.
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
[step addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventValueChanged];
- (void) someAction:(UIStepper *) stepper {
NSLog(#"stepper clicked");
}
I subclassed UITableViewCell and added a button to it. How do I response to the event when the button is touched? I also need to know which index path was the button pressed on.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomTableCell";
CustomTableCell *cell = (CustomTableCell *)
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"CustomTableCell"
owner:nil options:nil];
for (id currentObjects in topLevelObjects){
if ([currentObjects isKindOfClass:[StatusTableCell class]]){
cell = (StatusTableCell *) currentObjects;
break;
}
}
}
cell.customButton.tag = indexPath.row;
return cell;
}
If you have added the UIButton by code in your UITableViewCell Subclass you can use the following code to add an action for it.
//In UITableViewCell subclass
[customButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
And then you have already written the code for accessing the row number by setting,
cell.customButton.tag = indexPath.row;
You can get the tag value in the aMethod and access the indexPath.row value like below,
//In UITableViewCell subclass
-(IBAction)aMethod:(UIButton*)button
{
NSLog(#"indexPath.row value : %d", button.tag);
}
You can assign the IBAction in the CustomTableCell Nib and in the function do something like this:
- (IBAction) btnDoSomethingPressed:(id)sender {
NSIndexPath *indexPath = [yourTableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
//Use indexPath (or indexPath.row) like you would in a UITableViewDelegate method
}
This assumes that yourTableView is an instance variable assigned to your table.
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?
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.