I am using custom cell and adding tableview programmetically. I am trying to implement swipe functinality in my application. The tableview datasource method are not called when I try to swipe.
tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
tableview.delegate=self;
tableview.dataSource=self;
tableview.backgroundColor=[UIColor clearColor];
tableview.backgroundColor=selectedBackGroundColor;
tableview.separatorColor = [UIColor clearColor];
tableview.editing=YES;
NSLog(#"%f %f",self.view.frame.size.width,self.view.frame.size.height);
[self.view addSubview:tableview];
and this method is calling
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
{
return UITableViewCellEditingStyleDelete;
}
after that even cell is also not selecting...am i missing any thing code please help me out?
-(void)layoutSubviews
{
[super layoutSubviews];
}
Then Its Working Fine.....Thanks For answering my Question
Did you implement -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath in the datasource.
If you have set the tableview to editing already I doubt that swipe to delete (and selections if allowsSelectionDuringEditing is NO) will work at all. Try removing this line:
tableview.editing=YES;
and see if the swiping is enabled.
use this implementation instead:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete an object
}
}
Related
I have a design issue with grouped uitableview, I add uiviews to the leftmost side of each cell extending little over the cell bounds and I want them to be clipped. But neither setting clip subviews in IB nor setting clipsToBounds property in code didn't help.
So far I have set clipsToBounds (for both uitableview itself and individual uitabviewcells) in a number of places but none helped:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell setClipsToBounds:TRUE];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultIdentifier];
if (cell == nil)
{
cell = [self reuseTableViewCellWithIdentifier:defaultIdentifier withIndexPath:indexPath];
}
...
[cell setClipsToBounds:TRUE];
...
return cell;
}
- (void)viewDidLoad
{
...
[mainTableView setClipsToBounds:TRUE];
}
Is it possible without changing tableview style to plain?
Update: My previous answer was wrong. Here is an answer that actually shows how to do what you're asking.
https://stackoverflow.com/a/401271/350467
.......
Previous Answer
If you're adding views to the cell's contentView (which you should be, unless you have a good reason), you'll want to set cell.contentView.clipsToBounds = YES;
Not long ago I asked about how to move reorderControl view close to the center of my custom cell, so this reorderControl break custom design. As I can't move this view and my cell always in editing mode - I just draw this reorderControl in right place of my cell background. But now I have two reorderControls ) one from my background in right place and one from iOS. If I set return value of - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath to NO, iOS reorderControl disappear but I can't reorder rows anymore.
How can I remove iOS reorderControl from screen and still be able to reorder my custom UITableViewCell?
ps cell.showsReorderControl=NO; is not working
It's kind of a hack but the following does work
for(UIView* view in self.subviews)
{
if([[[view class] description] isEqualToString:#"UITableViewCellReorderControl"])
{
for(UIImageView* cellGrip in view.subviews)
{
if([cellGrip isKindOfClass:[UIImageView class]])
[cellGrip setImage:nil];
}
}
}
The solution I got it work in iOS7 is to add another method to find reorder control. Since the tableview structure has changed in iOS7 It was a little bit hard to track UITableViewCellReorderControl. It is now in UITableViewCellScrollView.
The solution is like below
- (void)changeReorderControl:(UITableViewCell *)cell subviewCell:(UIView *)subviewCell
{
if([[[subviewCell class] description] isEqualToString:#"UITableViewCellReorderControl"]) {
[subviewCell.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIImageView *imageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hand"]];
imageView.center = CGPointMake(subviewCell.frame.size.width/2 , subviewCell.frame.size.height/2);
[subviewCell addSubview:imageView];
}
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
for(UIView* subviewCell in cell.subviews)
{
if([[[subviewCell class] description] isEqualToString:#"UITableViewCellScrollView"]) {
for(UIView* subSubviewCell in subviewCell.subviews) {
[self changeReorderControl:cell subviewCell:subSubviewCell];
}
}
}
}
Hope you will get the idea.
I'm trying to use a UITableViewController and replace the editing mode button (usual default is 'Delete') with a custom view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... (other code) ...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag = pd.itemId;
UIImage *indicatorImage = [UIImage imageNamed:#"indicator.png"];
cell.editingAccessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];
//cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
When I try to swipe, nothing happens. What's strange is that if I uncomment the cell.accessoryView line, my image appears fine. This makes me think that it something about the editing settings that is wrong? But nowhere online or in documentation can I find what those settings are supposed to be.
Thanks!
Update: Ah. okay, so I gave myself an 'edit' button for the UITableView nav controller. Now I can toggle all the cells into edit mode, and my accessory appears. What does 'swiping' a cell do then?
Kurt
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
canEditRowAtIndexPath should return YES if you set the editingAccessoryView and want it to appear. Plain accessoryView is visible while the table is not in editing mode. However the swiping of the cell shows the delete confirmation button which isn't the editingAccessoryView, and messing with
- (void)willTransitionToState:(UITableViewCellStateMask)state;
without calling [super willTransitionToState:state]; will lead to unexpected behavior (your cell will not leave the UITableViewCellStateShowingDeleteConfirmationMask state). If you can manage calling super and not allowing the cell to show the delete confirmation button, then you're done.
I am trying to default the top cell in my table view to be highlighted whenever the subview appears.
I tried to do this:
switch(indexPath.row) { // assuming there is only one section
case 0:
cell.textLabel.text = #"First Cell:";
cell.highlighted = YES
break;
But this doesn't work as I get a black overlay on my first cell instead.
EDIT (added screenshot)
Any advise on this will be greatly appreciated!
Thanks
Zhen
I had the exact same problem. You need to move your code into willDisplayCell, instead of cellForRowAtIndexPath.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0) {
[cell setSelected:YES animated:NO];
}
}
I think what you need is,
cell.selected = YES;
If you want it animated you can use,
[cell setSelected:YES animated:YES];
Is there a way to change the order of tableviewcells with animation?
I want to slide up or down cells and change the ordering.
But i dont know if it is possible with animation?
I need help.
Thanks,Tintin
You could do that UITableView's edit mode.
Iphone sdk tutorial Add/Delete/Reorder UITableView row.
In this code dataArray is the name of my array you change it with your own array name
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSString *stringToMove = [[self.dataArray objectAtIndex:sourceIndexPath.row] retain];
[self.dataArray removeObjectAtIndex:sourceIndexPath.row];
[self.dataArray insertObject:stringToMove atIndex:destinationIndexPath.row];
[stringToMove release];
[tableView reloadData];
}