I've a UITableView, I am trying to delete a row when editing mode is active but commitEditingStyle is not fired.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.text=[NSString stringWithFormat:#"Row Number %d",indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"trying to delete a row..");
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 6;
}
-(void)Edit:(id)sender //Active editing mode
{
[self.table setEditing:YES animated:YES];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(Done:)];
}
I just want to delete a row?
This is how I show my popover:
UIPopoverController *popover;
-(IBAction)open:(id)sender
{
CGRect r=((UIButton*)sender).frame;
CGRect tRect=[((UIButton*)sender) convertRect:((UIButton*)sender).frame toView:self.view];
tRect.origin.x=r.origin.x;
Popover *firstViewCtrl = [[Popover alloc] init];
UINavigationController *navbar = [[UINavigationController alloc] initWithRootViewController:firstViewCtrl];
navbar.preferredContentSize = CGSizeMake(300, 300);
popover = [[UIPopoverController alloc] initWithContentViewController:navbar];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(300, 300);
CGRect popRect = CGRectMake(0,
0,
200,
200);
[popover presentPopoverFromRect:popRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
// [popover presentPopoverFromRect:tRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
I created UITableView using Xcode interface.
-(void)Done:(id)sender
{
[self.table setEditing:NO animated:NO];
//[self.table endEditing:true];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(Edit:)];
}
it fires when you touch DELETE button, not the minus... And delete buttons on your tableview doesn't show propably due to your tableview's width...
edit: after looking at your code, I think charty is correct.
be sure to check for the editing style, and make the necessary adjustments to your data source. something like:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"deleting");
[_resultsArray removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
}
Related
I am passing NSMutableArray to another a tableview and I want to show it in the table view. My NSMutableArray is as follows
2017-02-07 18:32:24.086 krib[13753:2978659] (
"Balcony.png",
"Utilities Included.png",
"Air-conditioning.png",
"Stove.png",
"WiFi Included.png",
"Queen Bed.png",
"Dining Table.png",
"Washing Machine.png",
"Dryer.png",
"Sofa.png",
"TV.png",
"Curtains.png",
"Refrigerator.png",
"Water Heater.png",
"Microwave Oven.png"
)
I send data as,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segue"]) {
MZFormSheetPresentationViewControllerSegue *presentationSegue = (id)segue;
presentationSegue.formSheetPresentationController.presentationController.shouldApplyBackgroundBlurEffect = YES;
UINavigationController *navigationController = (id)presentationSegue.formSheetPresentationController.contentViewController;
AmennitiesTableTableViewController *presentedViewController = [navigationController.viewControllers firstObject];
// presentedViewController.textFieldBecomeFirstResponder = YES;
presentedViewController.passingString = facilities;
}
and receive data in table view controller,
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(close)];
NSLog(#"%#",self.passingString);
[self.tableView reloadData];
}
I tried showing the mutable array as follows,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"facilitiesCell";
AmenitiesTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.facilitylbl.text = [self.passingString objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
But I am not understanding what exactly i am missing to Populate data on the tableview.
I think you are missing to set tableview delegate & datasource.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(close)];
[self.myTable setDelegate:self];
[self.myTable setDataSource:self];
NSLog(#"%#",self.passingString);
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.passingString.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"facilitiesCell";
AmenitiesTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(self.passingString.count > 0){
cell.facilitylbl.text = [self.passingString objectAtIndex:indexPath.row];
}
// Configure the cell...
return cell;
}
I am struck at a problem where I need to implement a multiple type selection picker so for this purpose i am doing this :
caratFromPicker = [[UIPickerView alloc] init];
caratTable = [[UITableView alloc]initWithFrame:caratFromPicker.frame style:UITableViewStylePlain];
caratTable.delegate = self;
caratTable.dataSource = self;
caratTable.bounces = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-caratFromPicker.frame.size.height-50, self.view.frame.size.width, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];
[toolBar setItems:toolbarItems];
price1.inputView = caratFromPicker;
price1.inputAccessoryView = toolBar;
[caratFromPicker setDataSource: self];
[caratFromPicker setDelegate: self];
caratFromPicker.showsSelectionIndicator = YES;//loadFromPicker
[caratFromPicker addSubview:caratTable];
and implemented the UITableView delegates as :
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [caratFromArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
cell.textLabel.text = [caratFromArray objectAtIndex:indexPath.row];
return cell;
}
here is the screenshot of the same :
but my problem is I am not able to scroll the tableview to view next values.
You can try using UIPicker delegate method :
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)theView{
return YourTableView;
}
To set tableView as the view for the row.
I created a UITableView programmatically and set all of its attributes, but the data from my NSMutableArray will not populate the cells and the cells do nothing when tapped. Any insight appreciated, thanks!
Clayton
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
list = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * .3333, self.view.bounds.size.width, self.view.bounds.size.height * .6667) style:UITableViewStylePlain];
list.delegate = self;
list.dataSource = self;
list.backgroundColor = [UIColor magentaColor];
list.scrollEnabled = YES;
list.userInteractionEnabled = YES;
list.multipleTouchEnabled = YES;
list.hidden = NO;
[self.view addSubview:list];
[self.view bringSubviewToFront:list];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [favorites count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cell = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cell];
}
cell.textLabel.text = [[global sharedInstance].favorites objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
cell.userInteractionEnabled = YES;
cell.hidden = NO;
cell.multipleTouchEnabled = YES;
[self.view bringSubviewToFront:cell];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"selected %ld row", (long)indexPath.row);
}
Got it thanks, the problem was with the data transferring from my singleton array of favorites to my local array (which was previously empty). Much obliged :)
At the end of viewDidLoad method, just add this:
[list reloadData];
I have got an issue with getting Delete button on Edit one clicks. The problem is being that it doesn't appear at all. My code is below and it works for swiping across a cell... Thank you in advance for the help!
#import "ViewController.h"
#interface ViewController ()
#property (strong) NSArray *lessons;
#end
static NSString *identifier = #"Cell";
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.lessons = #[
#"Computer Science",
#"Math",
#"Chemistry"
];
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:identifier];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.lessons.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell.textLabel.text = self.lessons[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
#end
Once I pressed Edit button.
This appears when I swiped across the cell.
hear above coding see that its really useful for that.
- (HelloController *) init
{
if (!(self = [super init])) return self;
self.title = #"Table Edits";
// Initialize the table titles, so they can be edited over time
tableTitles = [[NSMutableArray alloc] init];
ithTitle = NCELLS;
for (int i = 1; i <= NCELLS; i++)
[tableTitles addObject:[NSString stringWithFormat:#"Table Cell #%d", i]];
return self;
}
#pragma mark UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView :(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableTitles count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"any-cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"any-cell"] autorelease];
}
cell.text = [tableTitles objectAtIndex:[indexPath row]];
// cell.editingStyle = UITableViewCellEditingStyleDelete; // now read-only and no longer needed
return cell;
}
- (void) add
{
[tableTitles addObject:[NSString stringWithFormat:#"Table Cell #%d", ++ithTitle]];
[self.tableView reloadData];
}
#pragma mark UITableViewDelegateMethods
- (void) deselect
{
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
// Respond to user selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
{
printf("User selected row %d\n", [newIndexPath row] + 1);
[self performSelector:#selector(deselect) withObject:nil afterDelay:0.5f];
}
-(void)leaveEditMode
{
// Add the edit button
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"Edit"
style:UIBarButtonItemStylePlain
target:self
action:#selector(enterEditMode)] autorelease];
[self.tableView endUpdates];
[self.tableView setEditing:NO animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
printf("About to delete item %d\n", [indexPath row]);
[tableTitles removeObjectAtIndex:[indexPath row]];
[tableView reloadData];
}
-(void)enterEditMode
{
// Add the edit button
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"Done"
style:UIBarButtonItemStylePlain
target:self
action:#selector(leaveEditMode)] autorelease];
[self.tableView setEditing:YES animated:YES];
// [self.tableView beginUpdates];
}
- (void)loadView
{
[super loadView];
// Add an add button
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"New"
style:UIBarButtonItemStylePlain
target:self
action:#selector(add)] autorelease];
// Add the edit button
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"Edit"
style:UIBarButtonItemStylePlain
target:self
action:#selector(enterEditMode)] autorelease];
}
If anyone has the same issue, then you are probably using a UITableView in a UIViewController, so it doesn't manage editions for you. When you click Edit button, you must implement TableView's editing mode AND View's editing mode manually. How have I solved that issue.
#pragma mark - Edit button listener
- (void)editButtonPressed {
if(self.editing) {
[self setEditing:NO animated:YES];
[self.tableView setEditing:NO animated:YES];
} else {
[self setEditing:YES animated:YES];
[self.tableView setEditing:YES animated:YES];
}
}
Thank you guys for commenting!
I got a big problem.
[self.tableView reloadData];
Doesn't work, and I don't understand why.
[[self tableView] reloadData];
Doesn't work too.
Here is my code:
.h
#interface ArticleViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
}
#end
.m
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
btnLeft = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btnLeft"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(loadPlist)];
self.navigationItem.leftBarButtonItem = btnLeft;
In the loadPlist method, I'm writing in a .plist file. This part work fine.
Once all is write in the .plist file :
btnRight = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btnRight"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(reloadTheTB)];
self.navigationItem.rightBarButtonItem = btnRight;
- (void)reloadTheTB {
NSLog(#"Test reloadTheTB");
[[self tableView] reloadData];
}
If I touch btnRight, I can see in the log "Test reloadTheTB".
Here is tableView:cellForRowAtIndexPath:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [self getCellContentView:CellIdentifier];
contentDictio = [dict objectAtIndex:indexPath.row];
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
lblTemp1.text = [contentDictio objectForKey:#"title"];
if(indexPath.row % 2) {
UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_grise"]];
cell.backgroundView = myBackgroundView;
}
else {
UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_blanche"]];
cell.backgroundView = myBackgroundView;
}
}
return cell;
}
UPDATE:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dict count];
}
Help me please...
Actually I'm wondering this how you know that reloadData is not called?
Try to put some log in cellForRowAtIndexPath? to see if is called or not when you click your button? and also in do this :
- (void)reloadTheTB {
NSLog(#"Test reloadTheTB");
NSLog(#"%#",dict);
[[self tableView] reloadData];
}
Is the content of your dictionary what you expect? or is not changed after loadPlist?
Probably the call to reloadData is not executed on the main thread and you can update the UI only on the main thread. Try to execute your method on the main thread doing something like this :
-(void)ReloadTheTB{
[self.tableView performSelectorOnMainThread#selector(reloadData) withObject:nil waitUntilDone:NO]
}