Do not delete cell using the TableView section - ios

Hello I'm having trouble deleting a cell / record after it implemented the section in tableview.
See the code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"celula";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
alimento *p = [sessaoAlimento objectAtIndex:indexPath.section];
crudAlimento *obj = [[crudAlimento alloc] init];
NSArray *arrayAlime = [obj listarAlimentoSessao:[p tipoAlimento] filtro:srBar.text];
alimento *objAlimento = [arrayAlime objectAtIndex:indexPath.row];
NSString *registro = [NSString stringWithFormat:#"%# - %#", [objAlimento alimentoid], [objAlimento nomeAlimento]];
[[cell textLabel]setText:registro];
return cell;
}
detail is that I can not use object id to pass to the method
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
crudAlimento *deleteAlimento = [[crudAlimento alloc] init];
alimento *objUsuario = [arrayAlimento objectAtIndex:indexPath.row];
[deleteAlimento deletar:objUsuario];
[self carregaListaSessao:#""];//com sessao
}

I'm struggling a little with the language gap, but it looks like you are trying to delete an object from an empty crudAlimento object. You are creating a new one:
crudAlimento *deleteAlimento = [[crudAlimento alloc] init]; // this is empty unless you're overriding the init method in crudAlimento
And then deleting an item from it:
[deleteAlimento deletar:objUsuario]; // deleting an object from an empty source
From the looks of things, it's hard to tell if your data source for your table is mutable, but assuming it is, I think you need to remove a sub-element of sessaoAlimento (something like sessaoAlimento[indexPath.section].tipoAlimento[indexPath.row])

Related

How to make a TableView out of different NSObjects?

maybe somebody can help me. I Have different class objects. They'll get created in the TableView.m file with arrays as properties of these objects. How can I populate the TableView out of these Objects so that every cell contains on object? When I am adding the objects to an array I am getting an error, when I am running the simulator.
Error:
App1[6354:7304461] -[Object1 isEqualToString:]: unrecognized selector sent to instance 0x600000455c00
I think that the TableView doensn't know how to create the cell. But I don't have enough experience to know what is really going on.
Thanks
- (void)viewDidLoad {
[super viewDidLoad];
//creating first Object of NSObject Class Object1
Object1 *object1Class = [[WLAN alloc]init];
object1Class.object1PicsCreated = [[NSMutableArray alloc] init];
object1Class.object1TextsCreated = [[NSMutableArray alloc] init];
[object1Class.object1PicsCreated addObject:object1Class.picsObject1];
[object1Class.object1TextsCreated addObject:object1Class.textsObject1];
//creating second Object of NSObject Class Object2
Object1 *object1Class = [[WLAN alloc]init];
object2Class.object2PicsCreated = [[NSMutableArray alloc] init];
object2Class.object2TextsCreated = [[NSMutableArray alloc] init];
[object1Class.object2PicsCreated addObject:object2Class.picsObject2];
[object1Class.object2TextsCreated addObject:object2Class.textsObject2];
if (!_objects){
_objects = [[NSMutableArray alloc] init];
}
[_objects addObject:object1Class]
[_objects addObject:object2Class]
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = [_objects objectAtIndex:indexPath.row];
return cell;
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#",[_objects objectAtIndex:indexPath.row]];
Xcode crashes because you pass an instance of Object1 to the setter which expects NSString. UITableView thinks that object is NSString so calls NSString's methods against it (for you it was -isEqualToString:), which is a reason it crashes.
Whatever you want to show needs to be converted to NSString before you pass it there.
How can I populate the TableView out of these Objects so that every cell contains on object?
This is a very strange question. UITableView's rows are UITableViewCell-s. You may not populate a table with anything else.
Maybe, you should describe your tasks in more details.

Why same nsmutablearray gives me valid value and a null on different functions used

I have a NSMutableArray with some objects of type Notes i.e. my class with attributes, iD,note,noteTitle.. I am using the notes array to populate a tableview, and on click, I am trying to open another controller view, to show that specific table row clicked
My code are :
when controller load:
- (void)viewDidLoad {
[super viewDidLoad];
Notes * myNotes =[[Notes alloc] init];
notes = [myNotes getMyNotes];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *title = [NSString stringWithFormat:#"%#...",((Notes *) [notes objectAtIndex:indexPath.row]).noteTitle ];
// here i am using my notes nsmutablearray from above method to populate tableview list of titles.. and it is populated fine.
cell.textLabel.text = title;
cell.imageView.image=[UIImage imageNamed:#"back.jpg"];
return cell;
}
Now when I click a row, I am trying to just see if, I will be getting title, body and it for that certain note..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
long selectedRow = indexPath.row;
NSString *title = [NSString stringWithFormat:#"%#...",((Notes *) [notes objectAtIndex:selectedRow]).notes];
NSLog(#"%#",title);
}
But I am getting null this time...
why same code in above function is populating my table view but here not even logging it.
Thank you in advance....
You can try in didSelectRowAtIndexPath method
Notes *note = [[Notes alloc]init];
note = [notes objectAtIndex: indexPath.row];
NSString *title = [NSString stringWithFormat:#"%#...",note.notes];
NSLog(#"%#",title);
Hope it works.

how to show the sqlite data in uitableview on otherviewcontroller?

i have viewcontroller .h .m and .xib that contain registration form to insert update and delete.
and i have other xib that contain uitableview that show the sqlite data .how can i display ???
i write the code but its gives the error on array of viewcontroller page and other control that are used in viewcontroller.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[data objectAtIndex:indexPath.row] valueForKey:#"name"];
cell.detailTextLabel.text = [[data objectAtIndex:indexPath.row] valueForKey:#"salary"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
name.text = [[data objectAtIndex:indexPath.row] valueForKey:#"name"];
salary.text = [[data objectAtIndex:indexPath.row] valueForKey:#"salary"];
}
here data is NSArray in viewcontroller.h file gives error here
and also in didSelectRowAtIndexPath method name.text and salary.text gives error because it is the textfield of viewcontroller.
how can i solved it ?????
You have to make sure the data array should be available in otherViewController. For that create a array property in .h file of OtherViewController and while loading this view controller, assign the data array to this property.
Well as you are using SQlite database, you can fetch the data from the database(you'll need to add additional code) in OtherViewController and display it the tableView there.
Also you can't access the private variables of a view controller inside a different view controller. You will have to create properties for them or create public methods.
do it like this.. First create temporary dictionary and than fetch the data from that..
NSDictionary * tmpDictn = [data objectAtIndex:indexPath.row];
cell.textLabel.text = [tmpDictn valueForKey:#"name"];
cell.detailTextLabel.text = [tmpDictn valueForKey:#"salary"];
And same here..
NSDictionary * tmpDictn = [data objectAtIndex:indexPath.row];
name.text = [tmpDictn valueForKey:#"name"];
salary.text = [tmpDictn valueForKey:#"salary"];

Loading data from cell to detailView always show the last data

I have UITableView with some data from backend, I have everything in table,3 UILable and I will get that information from backend and I can show them in table row, when I click on row I want to have the same label in my detail view, but now, I have just one information in all my detail view, same information, just load the last row for all detailView,
would you please help me in this implementation,
Thanks in advance!
cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = #"BooksCell";
BooksCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell =[ [[NSBundle mainBundle] loadNibNamed:#"BooksCell" owner:nil options:nil]
lastObject];
}
_books = [server get_tests:10 offset:0 sort_by:0 search_for:#""];
_t = [NSMutableString stringWithFormat:#"%# ", [_books objectAtIndex:indexPath.row]];
NSString *testdata = _t;
_components = [testdata componentsSeparatedByString:#","];
NSLog(#"_components: %#",_components);
for (NSString *aComponent in _components) {
if ([aComponent hasPrefix:#"title"]) {
_subComponents = [aComponent componentsSeparatedByString:#":"];
_titleString = [_subComponents[1] stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:#"\""]];
}if ([aComponent hasPrefix:#"authors"]){
_subComponents = [aComponent componentsSeparatedByString:#":"];
_writerString = [_subComponents[1] stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:#"\""]];
}if ([aComponent hasPrefix:#"name"]){
_subComponents = [aComponent componentsSeparatedByString:#":"];
_publisherString = [_subComponents[1] stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#"\""]];
break;
}
}
cell.bookTitle.text = _titleString;
cell.writer.text = _writerString;
cell.publisher.text = _publisherString;
return cell;
}
didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
BooksDetailView *c = [[BooksDetailView alloc] init];
[self.booksTable addSubview:c.view];
c.bDetailTitle.text = _titleString;
c.bDetailWriter.text = _writerString;
c.bDetailPublisher.text = _publisherString;
}
_titleString, _writerString and _publisherString seem to be instance variables
of the table view controller, and these are overwritten in cellForRowAtIndexPath
each time a cell is displayed.
You have to use the indexPath in didSelectRowAtIndexPath to get the correct element
of your data source.
Note also that fetching all elements from the server in cellForRowAtIndexPath
is very ineffective, because this method is called frequently.
You should fetch the data once (e.g. in viewDidLoad) and assign
the fetched array to an instance variable or property of the view controller.
Then you can access the elements from the array in cellForRowAtIndexPath and in
didSelectRowAtIndexPath.
Add a property
#property (strong, nonatomic) NSArray *books;
to the view controller. Fetch the data in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.books = [server get_tests:10 offset:0 sort_by:0 search_for:#""];
// ... other stuff ...
}
In cellForRowAtIndexPath you just get the element from the array.
Also, instead of all that string manipulation, you should use the model class
and property accessors generated by the Thrift API.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = #"BooksCell";
BooksCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell =[[[NSBundle mainBundle] loadNibNamed:#"BooksCell" owner:nil options:nil] lastObject];
}
YourModelClass *book = self.books[indexPath.row];
cell.bookTitle.text = book.title;
// ...
return cell;
}
And similarly in didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
BooksDetailView *c = [[BooksDetailView alloc] init];
[self.booksTable addSubview:c.view]; // WHY THAT?
YourModelClass *book = self.books[indexPath.row];
c.bDetailTitle.text = book.title;
// ...
}

Accessing dictionaries in PList for UITableView

I'd like to create a simple reference app that lists a group of people, their job title, and their portrait. What I have so far is the list of people and their job title. It works alright, but I think I should have done it differently.
From reading other posts, I suppose I should be using dictionaries. This is how my PList currently looks:
And this is how the important bits of my code look:
#implementation RootViewController
#synthesize staffArray, subtitleArray;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* path = [[NSBundle mainBundle] pathForResource:#"StaffData" ofType:#"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSMutableArray *tmpNameArray = [dict objectForKey:#"Root"];
self.staffArray = [[NSMutableArray alloc] initWithArray:tmpNameArray copyItems:YES];
NSMutableArray* tmpSubtitleArray = [dict objectForKey:#"Subs"];
self.subtitleArray = [[NSMutableArray alloc] initWithArray:tmpSubtitleArray copyItems:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [staffArray count];
}
// 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 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [staffArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [subtitleArray objectAtIndex:indexPath.row];
return cell;
}
Using two arrays kind of defeats the purpose of OOP, I think, because in this case the people aren't connected to their job titles; they just happen to be in the same order. I'd like to create for example:
Array called Jonas, first value = job title, second value = pathToImage.png.
Another array called Andreas, etc etc etc.
What do I do?
I think that as a start, your design lacks an "Employee" object, that has data members like "Name", "JobTitle", etc... After you have this set up, just create an array of people and take whatever you need from there, by index.

Resources