Cell repeating data after 3rd cell when we add new cell - ios

I am working on an image editing iPhone application. I am using UITableView. My problem is that when I insert new rows into the table view, the new table view cells after the 3rd cell repeat the data of the 1st cell.
I have to do lot of searches and unfortunately couldn't find the solution. Can anyone view my code and tell me where I've gone wrong?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"cell";
VerticalFrameCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[VerticalFrameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.imgView.image = nil;
cell.tf_Comment.text = nil;
}
//add gesture on picture
UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapHandlert:)];
imageTap.delegate = self;
[cell.imgView addGestureRecognizer:imageTap];
//add pinch gesture for zooming image
UIPinchGestureRecognizer* pinchRecognizer =
[[UIPinchGestureRecognizer alloc] initWithTarget:self
action:#selector(handlePinch:)];
pinchRecognizer.delegate = self;
[cell.imgView addGestureRecognizer:pinchRecognizer];
//add pan gesture for plcing text
UIPanGestureRecognizer *moveText = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveTextOnImge:)];
moveText.delegate = self;
[moveText setMinimumNumberOfTouches:1];
[moveText setMaximumNumberOfTouches:1];
[cell.tf_Comment addGestureRecognizer:moveText];
cell.imgView.tag = indexPath.row+6000;
cell.sclView.tag = indexPath.row+7000;
cell.btnAddText.tag = indexPath.row+8000;
cell.tf_Comment.tag = indexPath.row+9000;
return cell;
}
// add new cell code here
- (IBAction)btnAddNewCell_Action:(id)sender {
if (objectsArray.count<6) {// not add more then six cell
[objectsArray addObject:#"Other's"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:objectsArray.count-1 inSection:0];
[tbl_Frame insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[tbl_Frame scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}else{
[AppUtility displayAlertWithTitle:AlertTitle AndMessage:#"You can't take more then six images"];
}
}

Related

How to take check box in table view only selected item on didSelectRowAtIndexPath

How to take selected item in table view didSelectRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if ([selectedRowsArray containsObject:[contentArray objectAtIndex:indexPath.row]]) {
cell.imageView.image = [UIImage imageNamed:#"checked.png"];
}
else {
cell.imageView.image = [UIImage imageNamed:#"unchecked.png"];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleChecking:)];
[cell.imageView addGestureRecognizer:tap];
cell.imageView.userInteractionEnabled = YES; //added based on #John 's comment
//[tap release];
cell.textLabel.text = [contentArray objectAtIndex:indexPath.row];
return cell;
}
- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer {
CGPoint tapLocation = [tapRecognizer locationInView:self.tableView];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
if ([selectedRowsArray containsObject:[contentArray objectAtIndex:tappedIndexPath.row]]) {
[selectedRowsArray removeObject:[contentArray objectAtIndex:tappedIndexPath.row]];
}
else {
[selectedRowsArray addObject:[contentArray objectAtIndex:tappedIndexPath.row]];
}
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}
This wll return all indexpaths of selected rows
tableView.indexPathsForSelectedRows

How to show a view created inside the custom tableview cell by swiping the row?

I have created the iPhone app which has the custom tableview cell and I also created a view in cellForRowAtIndexPath. Now, when i swiping the the cell in UITable the view that i created should be displayed in the particular cell and all other cell contents should be remained same.
My code as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RKCardCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"RKCardCell"];
// Configure the cell...
if (cell == nil) {
cell = [[RKCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"RKCardCell"];
}
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeMethod:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:swipeGesture];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-320.0, 8.0, 300, 180)];
[view setBackgroundColor:[UIColor greenColor]];
cell.profileImage.image = [UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]];
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.descriptionLabel.text = [descriptionArray objectAtIndex:indexPath.row];
//%%% I made the cards pseudo dynamic, so I'm asking the cards to change their frames depending on the height of the cell
cell.cardView.frame = CGRectMake(10, 5, 300, [((NSNumber*)[cardSizeArray objectAtIndex:indexPath.row])intValue]-10);
return cell;
}
Following code is for gesture swiping:
- (void)swipeMethod:(UIGestureRecognizer *)gestureRec
{
[UIView animateWithDuration: 0.5 animations:^{
CGRectOffset(self.rkCardCell.cardView.frame, -320.0, 0.0);
}];
}
Thanks in advance.
It is good if you use the gesture recognizer with the table view.
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeTarget:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:_myTableView];
And your swipe target method like as follows.
- (IBAction)swipeTarget:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint swipePoint = [gestureRecognizer locationInView:_myTableView];
NSIndexPath *cellIndexPath = [_myTableView indexPathForRowAtPoint:swipePoint];
RKCardCell *cell = [_myTableView cellForRowAtIndexPath:cellIndexPath];
// Add your code here to show your view
[UIView animateWithDuration: 0.5 animations:^{
CGRectOffset(cell.cardView.frame, -320.0, 0.0);
}];
}
Take a look on https://github.com/CEWendel/SWTableViewCell .
It's a really good and simple library to make custom swipable tableviewcell.

How to use swipe to change the label of specific row in tableview

In ViewDidLoad of table view controller, I first added SwipeGuesture and handler "Swipecell"
UISwipeGestureRecognizer *showExtrasSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(Swipecell:)];
showExtrasSwipe.direction = UISwipeGestureRecognizerDirectionRight|
UISwipeGestureRecognizerDirectionLeft;
[self.tableView addGestureRecognizer:showExtrasSwipe];
//Here I define the handler:
-(void)Swipecell:(UISwipeGestureRecognizer *)gesture
{
//Taking gesture location
CGPoint location = [gesture locationInView:_tableview];
//Getting IndexPath for that location in tableview
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
NSString *data = [self.dataArray objectAtIndex:swipedIndexPath.row];
//Setting the label of the cell
[swipedCell.textLabel setText:data];
}
But it is working only for one first row. Can anybody help me in this or give me better solution? Don't forget to rate, if you like the question.
Please add the swipe gesture in cellForRowAtIndexPath method , so the gesture will be added to each cell.
It works... Tested on iOS 7.1 simulator
- (void)viewDidLoad
{
_tableview = (UITableView*)self.view;
[super viewDidLoad];
UISwipeGestureRecognizer *showExtrasSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(Swipecell:)];
showExtrasSwipe.direction = UISwipeGestureRecognizerDirectionRight|
UISwipeGestureRecognizerDirectionLeft;
[self.tableView addGestureRecognizer:showExtrasSwipe];
}
-(void)Swipecell:(UISwipeGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:_tableview];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
swipedCell.detailTextLabel.text = [NSString stringWithFormat:#"%i", swipedIndexPath.row];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.textLabel.text = [NSString stringWithFormat:#"%i", indexPath.row];
cell.detailTextLabel.text = #" ";
return cell;
}
Change [gesture locationInView:_tableview] to [gesture locationInView:self.tableview]. I was refering to tableView outlet.

Static table view cells seem to have the same data and cell selected index?

Basically I need two things to work with the cells in my view, a tap and a tap and hold gesture. I have the tap and hold gesture working like so:
-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer
{
NSLog(#"gestureRecognizer= %#",gestureRecognizer);
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan)
{
NSLog(#"longTap began");
CGPoint p = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [myTable indexPathForRowAtPoint:p];
if (indexPath == nil)
{
NSLog(#"long press on table view but not on a row");
}
else
{
NSLog(#"long press on table view at row %d", indexPath.row);
switch (indexPath.row)
{
case 0:
del.tableRowNumber = 0;
break;
case 1:
del.tableRowNumber = 1;
break;
case 2:
del.tableRowNumber = 2;
break;
case 3:
del.tableRowNumber = 3;
break;
case 4:
del.tableRowNumber = 4;
break;
case 5:
del.tableRowNumber = 5;
break;
}
}
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"MealPlannerRecipeTypeViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
}
I need this gesture to set a value in a singleton class to a certain value depending on what row is selected. No matter what row is selected this value is always 0?! Can anyone tell me why?
Second part of the question is that one of my tableview delegates looks like this:
- (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];
RecipeInfo *recipeInfo = recipeInfoArray[indexPath.row];
cell.textLabel.text = recipeInfo.name;
// Add long tap for the main tiles
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTap:)];
[cell addGestureRecognizer:longPressGesture];
}
return cell;
}
Every row in my table view has the same information as the first one? Why is this?
Thanks
Since you're using reusable cells, and you've changed the textLabel inside the if() alloc-init block.
Move the cell.textLabel.text line out of the if() { } block right above return cell;
if (cell == nil) {
cell = ...
}
RecipeInfo *recipeInfo = recipeInfoArray[indexPath.row]
cell.textLabel.text = recipeInfo.name;
return cell;
}
EDIT: The above was for question 2.
The first question, about your gesture recognizer...looking at your code, you have
[gestureRecognizer locationInView:self.tableView];
But later you write
[myTable indexPathForRowAtPoint
Did you think to change locationInView:self.tableView to locationInView:myTable
You set cell values only when you created it. The "dequeue" can return a already instantiated cell. I purpose that move if bracket.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// Add long tap for the main tiles
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTap:)];
[cell addGestureRecognizer:longPressGesture];
}
RecipeInfo *recipeInfo = recipeInfoArray[indexPath.row];
cell.textLabel.text = recipeInfo.name;
...

UITapGestureRecognizer on UIImageView within UITablevlewCell not getting called

I currently have a custom UITableViewCell which contains a UIImageView and trying to add a UITapGestureRecognizer on the UIImageView with no luck. here is snippet of the code.
//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[tap release];
// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
RKLogDebug(#"imaged tab");
}
I've also set userInteractionEnabled on the cell and the superview of the UIImageView but still no luck, any hints?
EDIT:
I've also remove cell's selection by cell.selectionStyle = UITableViewCellSelectionStyleNone; Could this be a problem
UIImageView's user interaction is disabled by default. You have to enable it explicitly to make it respond to touches.
imageView.userInteractionEnabled = YES;
Swift 3
This worked for me:
self.isUserInteractionEnabled = true
In my case it looks like :
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = CELL_ROUTE_IDENTIFIER;
RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[RouteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
if ([self.routes count] > 0) {
Route *route = [self.routes objectAtIndex:indexPath.row];
UITapGestureRecognizer *singleTapOwner = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(imageOwnerTapped:)];
singleTapOwner.numberOfTapsRequired = 1;
singleTapOwner.cancelsTouchesInView = YES;
[cell.ownerImageView setUserInteractionEnabled:YES];
[cell.ownerImageView addGestureRecognizer:singleTapOwner];
} else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
And selector :
- (void)imageOwnerTapped:(UISwipeGestureRecognizer *)gesture {
CGPoint location = [gesture locationInView:self.tableView];
NSIndexPath *tapedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *tapedCell = [self.tableView cellForRowAtIndexPath:tapedIndexPath];
NSIndexPath *indexPath = [self.tableView indexPathForCell:tapedCell];
NSUInteger index = [indexPath row];
Route *route = [self.routes objectAtIndex:index];
}

Resources