TableView Cell contents not shifting in editing mode iOS 6 - uitableview

I have subclass of UITableViewCell. Here is my setup
I have used Autolayout. Now when user swipe on cell , I can see "Delete" button , but it overlaps my right side imageview. Cell contents are not shifting. Here is my code
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recentItemsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"ProductCell";
ProductCell *cell = (ProductCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:#"ProductCell" owner:self options:nil];
cell=[cellArray objectAtIndex:0];
tableView.separatorColor = [UIColor clearColor];
}
Product *product = [self.recentItemsArray objectAtIndex:indexPath.row];
cell.lblProductName.text = product.name;
cell.lblNoOfBids.text = [NSString stringWithFormat:#"%#",product.numberOfBids];
cell.lblBuyItNowPrice.text = [NSString stringWithFormat:#"%# %#",CURRENCY_TYPE,product.buyItNowPrice];
cell.lblTimeRemaining.text = product.timeRemaining;
cell.lblLatestBid.text = [NSString stringWithFormat:#"%# %#",CURRENCY_TYPE,product.currentBidPrice];
cell.lblUserName.text = product.sellerName;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110.0f;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
[CommonUtils showAlertWithTitle:#"Alert" andMessage:#"Do you want to delete this product?"];
}
}
I saw several question on Stackoverflow , but didn't get any solution.
Can any body tell me what I am doing wrong ? Any kind of help is highly appreciated. Thanks
Update::
I tried adding UIImageView programatically. still image views are not shifting
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(260, 0, 50, 60)];
imageView.backgroundColor = [UIColor yellowColor];
[cell.contentView addSubview:imageView];
UIImageView* imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(20.0, 0, 60, 60)];
imageView1.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:imageView1];
}
//cell.textLabel.text = #"ABC";
// Configure the cell...
return cell;
}

It happend with me. Verify if some cell property have the same name from other property in the view where the UITableView is.

Related

UITableView on scrolling goes wrong

I have created UITableView on UIView Class.
Based on the selection row setting the alpha value of UIImageView to 1.0f
on deselect the row setting alpha value to 0.2f, which work good.
But on scrolling the selected value (i.e alpha 1.0f) is highlighted with wrong cell, which was not selected at all.
Please find the below code which i have implemented.
Your feedback will be appreciated.
// Code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [colorNameList count]; // count is century.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self loadMoreTableViewCellForTableView:tableView indexPath:indexPath];
}
- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"FilterColorTableViewCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.filterColorImageView setBackgroundColor:[UIColor colorWithHexString:[[[ColorModelClass colorListNames]allValues] objectAtIndex:indexPath.row]alpha:1]];
cell.lbl_FilterColorName.text = [colorNameList objectAtIndex:indexPath.row];
cell.lbl_FilterColorCount.text = [NSString stringWithFormat:#"Items: %ld",(long)indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
FilterColorTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.filterColorTableView.allowsMultipleSelection = YES;
cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f);
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
FilterColorTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f);
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
You should also use this line of code:
cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f);
in your
- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
because it reuses the cell and this is why your previous settings are not reset.
If I understood you correctly, cell.filterSelectionColor.alpha is not correct as soon as you scroll through the TableView, isn't it?
You are relying on the Cell selected property, though cell position may not be the same as when they were created when scrolling. You should store the selected cells somewhere else (an array or so) and refresh Cell's status in cellForRowAtIndexPath. Something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self loadMoreTableViewCellForTableView:tableView indexPath:indexPath];
}
- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"FilterColorTableViewCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.filterColorImageView setBackgroundColor:[UIColor colorWithHexString:[[[ColorModelClass colorListNames]allValues] objectAtIndex:indexPath.row]alpha:1]];
cell.lbl_FilterColorName.text = [colorNameList objectAtIndex:indexPath.row];
cell.lbl_FilterColorCount.text = [NSString stringWithFormat:#"Items: %ld",(long)indexPath.row];
// selectedItems is an array of booleans with as many elements as the TableView
cell.filterSelectionColor.alpha = self.selectedItems[indexPath.row] ? 1.0f : 0.2f;
return cell;
}
When tableView reloads it uses the same cell instance and changes the data. When you reload/scroll your cellForRowAtIndexpath method is called and over there you will have to specify which cell should have which alpha. Below change in your code is required :
- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"FilterColorTableViewCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.filterColorImageView setBackgroundColor:[UIColor colorWithHexString:[[[ColorModelClass colorListNames]allValues] objectAtIndex:indexPath.row]alpha:1]];
cell.lbl_FilterColorName.text = [colorNameList objectAtIndex:indexPath.row];
cell.lbl_FilterColorCount.text = [NSString stringWithFormat:#"Items: %ld",(long)indexPath.row];
// Set alpha here
cell.filterSelectionColor.alpha = (cell.selected ?1.0f:0.2f);
return cell;
}
create an int property in your controller
NSMutableArray *selectedCells;
initialize the variable..
- (void)viewDidload {
...
...
selectedCells = [NSMutableArray array];
}
set the value on selection and deselection..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
...
...
[selectedCells addObject:[NSNumber numberWithInt:indexPath.row]];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
...
...
for (int i=0;i<selectedCells.count;i++) {
if([selectedCells[i] intValue] == indexPath.row)
[selectedCells removeObjectAtIndex:i];
}
}
As the cells are reused by the tableView.. you need to setup the cell from the cellForRow method..
- (FilterColorTableViewCell *)loadMoreTableViewCellForTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
FilterColorTableViewCell *cell = (FilterColorTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"FilterColorTableViewCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
...
...
BOOL selected = [selectedCells containsObject:[NSNumber numberWithInt:indexPath.row]];
cell.filterSelectionColor.alpha = (selected) ?1.0f:0.2f;
return cell;
}

table view cells with background image not filling the whole width of screen?

Hi i am new for ios and in my app i have created one UITableView and i have set background image for UITableViewcell but image not filling the whole width of screen as like below screen. Why this problem is occuring?
I mean UITableViewCell left and right sides gap is coming images is not filling whole cell width.
please help me someone
my code:-
#import "TableViewController.h"
#interface TableViewController ()
{
UITableView * tableList;
TableCell * Cell;
}
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
tableList = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height) style:UITableViewStylePlain];
tableList.delegate = self;
tableList.dataSource = self;
tableList.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tableList];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"MyCell";
Cell = (TableCell *)[tableList dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (Cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableCell" owner:self options:nil];
Cell = [nib objectAtIndex:0];
}
//UIImageView *imageBackground = [[UIImageView alloc] init];
if (indexPath.row == 0) {
Cell.backGroundImage.image = [UIImage imageNamed:#"cell_top.png"];
} else if (indexPath.row == 9) {
Cell.backGroundImage.image = [UIImage imageNamed:#"cell_bottom.png"];
} else {
Cell.backGroundImage.image = [UIImage imageNamed:#"cell_middle.png"];
}
//imageBackground.contentMode = UIViewContentModeScaleToFill;
//Cell.backgroundView = imageBackground;
return Cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44.0;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:#selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
#end
Sorry for my wrong post before. The issue you might be facing may be because of autolayouts. Check if the image view has proper autolayouts assigned or not.

How can we select tableList row bydefault

Hi i am very new for ios and in my app i have created
one UItablaList ok fine
here by default my tableList row one textcolor
i have assigned like "RED"
and when i click on another rows that first row color must be "WHITE" and selected row color must be "RED" and if i click dy default selected first row then that color must be "RED please help me some one ,How can we implement logic for this
my code:-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"MyCell";
MyCellTableViewCell *cell = (MyCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
UILabel *lineLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 55, RightMenuTbl.frame.size.width, 1)];
lineLbl.backgroundColor = [UIColor lightGrayColor];
[cell.contentView addSubview:lineLbl];
//remove background for table:-
if (indexPath.row == 0) {
lineLbl.textColor = [UIColor redColor];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
Take int as selectedRow in .h file and in didselectrow
selectedRow = indexPath.row;
[yourtableview reloadData];
and in cellforrow
if (selectedRow== indexPath.row) {
lineLbl.textColor = set color you want for selected
}else
lineLbl.textColor = set color you want for not
selected
try this code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath * cellIndexPath = indexPath;
SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:cellIndexPath];
cell.nameLbl.textColor = [UIColor redColor];
}
(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.nameLbl.textColor = [UIColor whiteColor];
}
Try the below code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.nameLbl.textColor = [UIColor redColor];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.nameLbl.textColor = [UIColor whiteColor];
}

iOS UITableViewCell Have To Hold A Long Press To Select Item

So this is driving me nuts, whenever I tap an item in my UITableView it does nothing, but when i press and hold the UITableViewCell after about 3-5 seconds it decides to move forward and do what I want.. any thoughts why this might be happening?
Here's my code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
_cell = [_arrayItems objectAtIndex:indexPath.row];
_cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
_cell = (CustomWidget *)[tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (_cell == nil) {
_cell = [[CustomWidget alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier title:[_arrayItems objectAtIndex:indexPath.row] subTitle:#"Custom subtitle"];
}
_cell.textLabel.text = [_arrayItems objectAtIndex:indexPath.row];
_cell.textLabel.hidden = YES;
return _cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arrayItems.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
_passedInPageTitle = selectedCell.textLabel.text;
[self openDetailPage];
}
I found my answer here - I had a UITapGestureRecognizer on the UITableView's parent view, which was capturing the tap.

Accessory type for Cross Sign in iOS

I have a table view and 2 custom cells inside it. I want to use a (X) symbol i.e. the cross symbol and the tick symbol. I know for tick we have UITableViewCellAccessoryCheckmark but what to do for the (X) symbol. Can we have any custom accessory for this or else how to achieve this?
My code:
#pragma mark - Table View Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
optionsLabelArray = [[NSArray alloc] initWithObjects:#"Yes", #"No", nil];
static NSString *CellIdentifier = #"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.optionLabel.text = [optionsLabelArray objectAtIndex:indexPath.row];
cell.optionLabelSubtitle.text = [optionsLabelSubtitleArray objectAtIndex:indexPath.row];
if(indexPath.row == 0)
{
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView ==_optionsTableView1) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
previousSelectedCell1.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType=UITableViewCellAccessoryCheckmark;
previousSelectedCell1 = cell;
NSLog(#"Row : %ld", (long)indexPath.row);
if(indexPath.row == 0)
{
self.weatherSafeToPlay = YES;
}
else
{
// MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
// sharedController.actions = [sharedController.actions stringByAppendingString:#"Check Weather. "];
//[sharedController.actions appendString:#"Check Weather. "];
self.weatherSafeToPlay = NO;
}
NSLog(#"Is Weather safe: %hhd", self.weatherSafeToPlay);
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
not heard of any accessory type for cross but in such cases, i simply use unicode characters.
It's lighter than using an image and is a simple matter of just changing the text.
#"\u2713" for ✓
#"\u2717" for ✗
You can:
Create a custom UITableViewCell having a 32by32 (or any dimension) UILabel (on the right/left/middle/wherever)
in -didSelectRowAtIndexPath, you can change the text of this UILabel
#"\u2713" for checkmark or #"\u2717" for crossmark
A simple example (on the default UITableViewCell's default textLabel, just for the idea):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([cell.textLabel.text isEqualToString:#"\u2713"]) {
cell.textLabel.text = #"\u2717";
}
else {
cell.textLabel.text = #"\u2713";
}
}
OR... when not using a custom cell, something like:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
//set your text to cell.textLabel.text
//create your own UILabel
UILabel *lblAcc = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
[lblAcc setTag:100];
[lblAcc setText:#"?"];
[lblAcc setBackgroundColor:[UIColor redColor]];
[lblAcc setTextAlignment:NSTextAlignmentCenter];
//set as custom AccessoryView on cell
[cell setAccessoryView:lblAcc];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *strAccessory = [(UILabel *) [cell viewWithTag:100] text];
//the -isEqualToString is a bit heavier than checking a simple bool or int value
//so since you should be remembering the selected cells anyways, you can change
//the if-logic to compare with a bool rather than the string comparison.
//but for now, we'll do:
if([strAccessory isEqualToString:#"\u2713"]) {
[(UILabel *) [cell viewWithTag:100] setText:#"\u2717"];
}
else {
[(UILabel *) [cell viewWithTag:100] setText:#"\u2713"];
}
}
for more unicode characters... this link:
http://en.wikibooks.org/wiki/Unicode/List_of_useful_symbols
You can assign your own image to accessoryView of table cell. Create an image of (X) symbol and use that image as accessoryView.
cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Your X Image"]];

Resources