I've been struggling with this problem for a good 2 hours now and I can not get it to work.
I have a view controller with a table view on it with a list of items. When the user touches the EDIT button in the navigation bar the table view should enter "edit mode" and under the list of items one new cell should appear that should only be visible when it is in "edit mode".
So in my -tableView:cellForRowAtIndexPath: method I'm checking if (tableView.isEditing) to setup the cell. I've tried overriding -setEditing:animated: and doing [tableView reloadData] calls but I just can't get it to work.
This is what my -setEditing:animated: look like:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.groupDetailsTableView setEditing:editing animated:animated];
}
My -tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (tableView.isEditing) {
// Other tableview code has been omitted
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
// Set the background view of the cell to be transparent
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setFrame:[cell.contentView frame]];
[deleteButton setFrame:CGRectMake(0, 0, cell.bounds.size.width-20, 44)];
[deleteButton setBackgroundImage:[UIImage imageNamed:#"redbutton.png"] forState:UIControlStateNormal];
[deleteButton setTitle:#"Delete Group" forState:UIControlStateNormal];
[deleteButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[deleteButton.titleLabel setShadowColor:[UIColor lightGrayColor]];
[deleteButton.titleLabel setShadowOffset:CGSizeMake(0, -1)];
[deleteButton addTarget:self action:#selector(deleteGroup) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteButton];
return cell;
}
}
And I'm calling [self setEditing:YES animated:YES]; from a method when the user touches the Edit button. I've also tried just calling [super setEditing:YES animated:YES]; and [self.groupDetailsTableView setEditing:YES animated:YES];
Found the answer myself at http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW19
Apparently -tableView:cellForRowAtIndexPath: doesn't get called automatically when calling -setEditing:animated:
So to have a new cell "appear" I set the alpha to 0.0 and then 1.0 after calling setEditing...
Related
I am creating a camera app. I am showing captured pictures in collection view. I placed a button to delete the particular picture. While running I am able to see the button to delete, but if I click the button it is not performing any action.
-(UICollectionViewCell *)collectionView:(UICollectionView *)
collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
Cell.self.image_View.image=[self.imageArray objectAtIndex:indexPath.row];
UIButton *deleteButton = [[UIButton alloc]init];
deleteButton.frame = CGRectMake(80, 0, 20, 20);
//deleteButton.backgroundColor = [UIColor redColor];
[deleteButton setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
[deleteButton setTag:indexPath.row];
[deleteButton addTarget:self action:#selector(delete:) forControlEvents:UIControlEventTouchUpInside];
[Cell.self.image_View addSubview:deleteButton];
return Cell;
}
-(void) delete:(UIButton*)sender{
UIButton *btn = (UIButton *)sender;
[self.imageArray removeObjectAtIndex:btn.tag];
//reload your collectionview here
}
Can anyone help me?
i think you might be missing
[collectionView reloadData]
-(void) delete:(UIButton*)sender{
UIButton *btn = (UIButton *)sender;
[self.imageArray removeObjectAtIndex:btn.tag];
[collectionView reloadData]
}
I have a UITableView with two cells that have two buttons in them. The cells need to be independent of one another. Both Button1's will be selected by default. I need to be able to toggle the buttons in the same cell without affecting the other cell. For example, if I click on Button2 in Option2, Button1 will be deselected in the same cell while Option3's buttons remain unaffected. The code I have to create the buttons within the UITableView is below. The selector would toggle the buttons. And each cell would execute a different method based on which button is selected. How can I group the buttons based on the cell that they are located in?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *menuId = #"OptionsMenu";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:menuId];
if (cell == nil)
cell = AUTO_RELEASE([[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:menuId]);
cell.textLabel.text = [self.options objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:#"Option2"] || [cell.textLabel.text isEqualToString:#"Option3"]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button1" forState:UIControlStateNormal];
button.tag = indexPath.row;
button.frame = CGRectMake(200.0f, 5.0f, 80.0f, 30.0f);
button.layer.borderWidth = 1.0f;
button.layer.cornerRadius = 4.0f;
button.layer.borderColor = [[UIColor grayColor] CGColor];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchDown];
[button2 setTitle:#"Button2" forState:UIControlStateNormal];
button2.tag = indexPath.row;
button2.frame = CGRectMake(300.0f, 5.0f, 80.0f, 30.0f);
button2.layer.borderWidth = 1.0f;
button2.layer.cornerRadius = 4.0f;
button2.layer.borderColor = [[UIColor grayColor] CGColor];
[cell addSubview:button];
[cell addSubview:button2];
}
return cell;
}
You want to make the cell the target of the control actions, not the table view. Then you can make the tableview a delegate of the cell. When the cell receives the setState: message, you have the cell tell the tableview via the delegate. You pass a pointer to the cell as part of the delegate message, and now the tableview knows what item to update in the model via indexPathForCell:.
i have a class loginViewController which has table contents. i have loaded a custom cell to each row of the table. in method cellForRowAtIndexPath i have created a button for each row and asigned it a tag value. clicking on this button takes us to new view controller logininsidesViewController. Now in logininsidesViewController class i want to access the button tag i created earlier.what should i do?
loginViewController.m class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
customcell *cell=(customcell*)[tableView dequeueReusableCellWithIdentifier:#"cellcstm"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(customActionPressed)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
[cell addSubview:button];
if (cell==nil)
{
//[tableView registerNib:[UINib nibWithNibName:#"customcell" bundle:nil] forCellReuseIdentifier:#"cellcstm"];
NSArray *arr12=[[NSBundle mainBundle]loadNibNamed:#"customcell" owner:self options:nil];
cell=[arr12 objectAtIndex:0];
}
for (int i=0; i<=indexPath.row; i++)
{
cell.selectionStyle=UITableViewCellEditingStyleNone;
}
if (indexPath.row==0)
{
cell.lbl.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=#"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if (indexPath.row==1)
{
button.tag=1;
cell.img.image=[UIImage imageNamed:#"facebook.png"];
cell.lbl.text=#"Facebook";
[cell.contentView addSubview:button];
}
if (indexPath.row==2)
{
button.tag=2;
cell.img.image=[UIImage imageNamed:#"g+.png"];
cell.lbl.text=#"Google";
[cell.contentView addSubview:button];
}
if (indexPath.row==3)
{
button.tag=3;
cell.img.image=[UIImage imageNamed:#"youtube.png"];
cell.lbl.text=#"Youtube";
[cell.contentView addSubview:button];
}
if (indexPath.row==4)
{
button.tag=4;
cell.img.image=[UIImage imageNamed:#"twitter.png"];
cell.lbl.text=#"Twitter";
[cell.contentView addSubview:button];
}
if (indexPath.row==5)
{
button.tag=5;
cell.img.image=[UIImage imageNamed:#"flicker.png"];
cell.lbl.text=#"Flicker";
[cell.contentView addSubview:button];
}
else
{
cell .textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}
i want to access the button.tag in my logininsidesViewController class. plz help
In continuation with Viruss's answer you can create a property with integer type in logininsidesViewController class and pass that from button click.
-(IBAction) customActionPressed:(id)sender{
NSLog(#"button tag:%d",[sender tag]);
logininsidesViewController *objLoginInside = [[logininsidesViewController alloc] initWithNibName:#"logininsidesViewController" bundle:nil];
objLoginInside.buttonTag = sender.tag;
[self.navigationController pushViewController:objLoginInside animated:NO];
}
Add One target method to your buttons and access it with tags,
In cellForRow method Update to this.
[button addTarget:self action:#selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
You can access here with tag,
-(IBAction) customActionPressed:(id)sender{
NSLog(#"button tag:%d",[sender tag]);
}
You are adding your button on cell and again you're adding it on cell's contentView correct that is the problem.
If you want adding it in cell.contentView you must provide allocation in each if condition and then add it
Edit: Do smart coding
viewDidLoad()
[self.myTableView registerNib:[UINib nibWithNibName:#"CViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
arImage = #[#"facebook.png", #"g+.png", #"twitter.png", #"flicker.png" ];
arLables = #[#"Facebook", #"GooglePlus", #"Tweeter", #"Flicker"];
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell * cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(customActionPressed) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
if (indexPath.row==0)
{
cell.lblText1.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=#"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if ( indexPath.row > 0 && indexPath.row <= 5) {
button.tag = indexPath.row;
cell.imageView.image=[UIImage imageNamed:arImage[indexPath.row - 1]];
cell.textLabel.text=[arLables objectAtIndex:indexPath.row - 1];
[cell.contentView addSubview:button];
} else {
cell.textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}
I have a table view cell with multiple images in them. When touching the images they shold display an overlay on top of the image which tells the user that this image was selected.
Is there a way to change the look of just one UITableViewCell without having to do a [tableView reloadData] which would allow me to style the cell differently in the table view datasource delegate method.
The way I would do it is to subclass UITableViewCell and then on tableView:didSelectRowAtIndexPath: get a reference to the cell and do whatever you want to it (or just target the image touch event if this is not a selection).
There might be another way of doing this without having to subclass, but I find myself subclassing UITableViewCell all the time and it's pretty straightforward to do.
If you wish to avoid subclassing, this can be achieved with gesture recognisers. Your question suggests a Tap and Hold user interaction on each image, which I have implemented in the code below. One point to remember, if the user is tapping and holding, they may not see the text you wish them to see.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"ImageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
UILongPressGestureRecognizer *recognizer2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Ben.png"]];
imageView.frame = CGRectMake(cell.contentView.bounds.origin.x,cell.contentView.bounds.origin.y , 100, 40);
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:recognizer];
[cell.contentView addSubview:imageView];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Steve.png"]];
imageView2.frame = CGRectMake(cell.contentView.bounds.origin.x + imageView.frame.size.width + 10,cell.contentView.bounds.origin.y , 100, 40);
imageView2.userInteractionEnabled = YES;
[imageView2 addGestureRecognizer:recognizer2];
[cell.contentView addSubview:imageView2];
[imageView release];
[imageView2 release];
[recognizer release];
[recognizer2 release];
return cell;}
- (void)imageTapped:(id)sender {
NSLog(#"%#", sender);
UILongPressGestureRecognizer *recognizer = (UILongPressGestureRecognizer *)sender;
if (recognizer.state == UIGestureRecognizerStateBegan) {
UILabel *label = [[UILabel alloc] initWithFrame:recognizer.view.bounds];
label.text = #"Pressed";
label.backgroundColor = [UIColor clearColor];
label.tag = 99999;
label.textColor = [UIColor whiteColor];
[recognizer.view addSubview:label];
[label release];
}
else {
[[recognizer.view viewWithTag:99999] removeFromSuperview];
}
}
Hope this helps.
well there is the
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
but this is for the cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
How can i catch a clickevent on the cell.Image?
UIImage view does not decsend from UIControl that's why it does not have Touchepinside like target action method. So what I guess you can do is Create a UIButton object. Then set an image for it's normal state:
[imageButton setImage:[UIImage imageNamed:#"Icon.png"]
forState:UIControlStateNormal];
Then set it's target action:
[imageButton addTarget:self action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
And then :
[cell.contentView addSubview:imageButton];
I guess that'll do the job.