My UITableViewCell will not delete - iphone

My UITableViewCell will not delete when I call the method. I can get it to print out the cell it is supposed to delete, but it will not remove itself, and it won't remove itself from the array.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
[customData removeObjectIdenticalTo:thisCell.textLabel.text];
NSLog(#"%#", customData);
[thisCell removeFromSuperview];
[tabler reloadData];
}

you need this: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

Related

Resizing the height of UILabel with the height of tableViewCell height

I am new to iOS development. I am using tableView with of dynamic height. The height of tableViewCell increases or decreases on the click for this I am using this code...
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.selectedPath isEqual:indexPath])
{
return 250;
}
else
{
return 44;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
onSelectCount++;
self.selectedPath = indexPath;
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation: UITableViewRowAnimationLeft];
self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row];
[self.tableView1 deselectRowAtIndexPath:indexPath animated:YES];
//First we check if a cell is already expanded.
//If it is we want to minimize make sure it is reloaded to minimize it back
if( onSelectCount==1 )
{
NSLog(#"num=%d",onSelectCount);
NSLog(#"First Condition");
[tableView beginUpdates];
NSIndexPath *previousPath = [NSIndexPath indexPathForRow:self.selectedRowIndex.integerValue inSection:0];
self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row];
self.selectedPath=indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
if(self.selectedPath.row!=indexPath.row)
{
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
selectedPath=indexPath;
onSelectCount=0;
[self tableView:tableView didSelectRowAtIndexPath:selectedPath];
}
if(self.selectedRowIndex.integerValue == indexPath.row && onSelectCount==2)
{
[tableView beginUpdates];
self.selectedRowIndex = [NSNumber numberWithInteger:-1];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
onSelectCount=0;
[tableView endUpdates];
}
}
Now I want to add some label to show some information on the tableViewCell but when I click on the cell it resizes perfectly but label on the cell does not resize.Please tell me how can I resize the UILabels with the Cell height. Any help will be appreciated...
Use the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to achieve this. It is also good to move the whole logic for changing the height for the cells from didSelectRow to cellForRowAtIndexPath.
To answer your question:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
CGRect sizeRect = cell.textLabel.frame;
sizeRect.size.height = cell.contentView.frame.size.height;
cell.textLabel.frame = sizeRect;
return cell;
}
where textLabel is the name of your label.
The basic idea is that you take the frame of the label, then set it's height to the height of cell's view and then set the new size to the label.

How to update a property of a custom UITableViewCell on didSelectRowAtIndex?

I have a UITableView that is made up of a number of custom UITableViewCells.
When the table didSelectRowAtIndexPath method fires, if the indexPath.row is equal to 2, I want to update a UILabel within the cell at this row.
My thinking was to use something along the following lines;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 2){
myCustomCellView *cell = [self.essentialsTableView cellForRowAtIndexPath:indexPath];
cell.myUILabel.text = #"my text";
[tableView reloadData];
}
}
The problem here is that cellForRowAtIndexPath returns an object of type UITableViewCell, not myCustomCellView.
Can anyone advise how i might access and update properties of cells within the didSelectRowAtIndexPath method?
Define one Bool value say "secondRowIsClicked". Set it to No in your viewWillApper or viewDidAppear function.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 2)
{
secondRowIsClicked = YES;
[tableView reloadData];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Your code
if (secondRowIsClicked == YES)
{
cell.myUILabel.text = #"my text";
}
}
try with bellow code..
myCustomCellView *cell = (myCustomCellView *) [self.essentialsTableView cellForRowAtIndexPath:indexPath];
you can reload UITableViewCell with its indexPath like bellow..
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
myCustomCellView *cell = [self.essentialsTableView cellForRowAtIndexPath:indexPath];
UILabel *yourLabel=[cell.contentViews.subviews viewWithTag:indexPath.row+1];
//do whatever you want with your label;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Your code
cell.myUILabel.text = #"my text";
cell.myUILabel.tag=indexpath.row+1;
}
typecast UITableViewCell to your custom cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 2){
myCustomCellView *cell = (myCustomCellView *)[self.essentialsTableView cellForRowAtIndexPath:indexPath];
cell.myUILabel.text = #"my text";
[tableView reloadData];
}
}
you need to upadte the datasoure and den call the reload the tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 2){
//update the array which is used as the data provider in cell for row at index path.
[tableView reloadData];
}
}
myCustomCellView is a subClass of UITableViewCell.
So just typcast the cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 2){
myCustomCellView *cell = (myCustomCellView *)[self.essentialsTableView cellForRowAtIndexPath:indexPath];
cell.myUILabel.text = #"my text";
[tableView reloadData];
}
}
More info:
As per Apple coding guide lines do not user class name start with lower case letter
instead of myCustomCellView use MyCustomCellView

Changing the image of the cell when it's checked

In my UITableView, i need to get the event of the user, so when the cell is selected(checked), i put btn_checked.png and when it's unchecked, i put the btn_unchecked.png.
I guess, what i try to do should be in the didSelectRowAtIndexPath method, so this is my code:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_unchecked.png"]];
theCell.accessoryView = imageView;
}
My snippet code doesn't work, so the btw_unchecked.png isn't placed when the row is checked, also, i need to get the event of checked/unchecked to place the right image to the right event.
Thanx in advance.
As an example, this code snippet will toggle the cell's checkmark when the cell is tapped. The pattern is to force a selective reload from the table's datasource when the delegate method is invoked.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue the cell... then:
static NSString *CellIdentifier = #"tvcOEList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryNone) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
You can use following code.
[cell.contentView addSubview:imageView];
i hope this will help you..

deleting cells in table View

I'm having a problem with my deleting of cells in the tableview , my program simply crashes when i press de delete button.
Here is my code for it :
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object at the given index path
sect = [listArray objectAtIndex:indexPath.row];
[sect removeObjectAtIndex:indexPath.row];
// Update Event objects array and table view
[listArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
tableView.editing = YES;
}
}
Remove the following completely:
sect = [listArray objectAtIndex:indexPath.row];
[sect removeObjectAtIndex:indexPath.row];
and
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
Just call [listArray removeObjectAtIndex:indexPath.row]; then call reloadData to refresh your UITableView.

tablerow delete but still showon in table view

my data is show on the table whn i select data to delet its delted from database but not from table , i go back to other view and again come to the delete table view then delete data is not show again
i am using this code ...
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPath withRowAnimation:(UITableViewRowAnimation)animation
{
NSLog(#"Hello");
}
-(void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// int row = [indexPath row];
[self.table beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
Hadits *delHadit = [self.allBookMarks objectAtIndex:indexPath.row];
dbAccess *dbmethods = [[dbAccess alloc] init];
NSInteger delHaditid = delHadit.haditid;
[dbmethods deleteBookMark:delHaditid];
[dbmethods release];
}
[self deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.table endUpdates];
NSLog(#"Hello");
[self.table reloadData];
}
...Help needed..
Regards Haseeb
in view will appear prepare your array which having record and in commitEditing add one line.
[self viewWillApear:YES];
and in viewWillAppear add this line
[yourTable reloadData];
after fetching data from db.
There is little mistake (or I think the relevant code is not shown). You have deleted the entry from the database, but I think you have missed to delete the same from your array which acts as your tableview datasource.
-(void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.table beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];
Hadits *delHadit = [self.allBookMarks objectAtIndex:indexPath.row];
dbAccess *dbmethods = [[dbAccess alloc] init];
NSInteger delHaditid = delHadit.haditid;
[dbmethods deleteBookMark:delHaditid];
[dbmethods release];
[self.allBookMarks removeObject:delHadit];/// change of code
}
//[self.table reloadData];
[table endUpdates];
}