is there a way to show Uitableviewcell selection on limited frame? - objective-c

i have a uitableview cell added some labels something 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIImage* myImage = [UIImage imageNamed:#"AccessoryForDealsMain.png"];
UIImageView *MyimageView = [[UIImageView alloc] initWithImage:myImage];
MyimageView.contentMode=UIViewContentModeScaleToFill;
cell.accessoryView=MyimageView;
[MyimageView release];
UILabel *BluePatti = [[UILabel alloc] init];
BluePatti.frame=CGRectMake(0,0,4,44);
BluePatti.backgroundColor = BLUEPATTI;//[UIColor blueColor];
UILabel *BlackLine = [[UILabel alloc] init];
BlackLine.frame=CGRectMake(3,0,1, 45);
BlackLine.backgroundColor = BLACK_LINE;
[BluePatti addSubview:BlackLine];
[BlackLine release];
[cell.contentView addSubview:BluePatti];
[BluePatti release];
UILabel *Seperator = [[UILabel alloc] initWithFrame:CGRectZero];
Seperator.backgroundColor = [UIColor colorWithRed:234/256.0 green:234/256.0 blue:234/256.0 alpha:1.0]; //[UIColor lightGrayColor];
Seperator.frame = CGRectMake(4,43,316,1);
[cell.contentView addSubview:Seperator];
[Seperator release];
}
if(indexPath.section==5)
{
cell.textLabel.text=[self.GenderCellData objectAtIndex:indexPath.row];
cell.textLabel.textColor=CELL_TEXT_COLOR;
cell.textLabel.font=CELL_FONT;
}
else
{
cell.textLabel.text=#"Cells";
cell.textLabel.font=CELL_FONT;
}
return cell;
}
now when i touch cell it shows blue selection above all subviews.How do i change selection frame and colour?

You can create a custom selectedBackgroundView in UITableViewCell that has your own frame and color. This will be shown when the cell is selected instead of the default blue background.
For example:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 30, 40)];
view.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = view;
[view release];

I face the same problem today , and I solved via this code
First , Inside my custom cell init
self.customerbgImageview = [UIImageView new];
[self.customerbgImageview setFrame:CGRectMake(0 , 11, Width, 66)];
[self.customerbgImageview setBackgroundColor:UIColor_RGB(0, 255, 255, 0.5)];
[self addSubview:self.customerbgImageview];
Second set selectionStyle none
cell.selectionStyle = UITableViewCellSelectionStyleNone;
third in the table method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomOrderTableViewCell *cell = (CustomOrderTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell.customerbgImageview setHidden:NO];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomOrderTableViewCell *cell = (CustomOrderTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell.customerbgImageview setHidden:YES];
}

Related

Unsupported configuration prototype table cells must have reuse identifiers

I build a UITableView in ViewDidLoad like this:
- (void)viewDidLoad
{
CGSize viewSize = self.view.frame.size;
mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 35, viewSize.width, viewSize.height -149)];
[mainScroll setPagingEnabled:YES];
[mainScroll setShowsHorizontalScrollIndicator:NO];
[mainScroll setDelegate:self];
[mainScroll setTag:101010];
// #1
UIScrollView *scroll1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, viewSize.width, viewSize.height -149)];
UITableView *table101 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, viewSize.width, viewSize.height -149) style:UITableViewStylePlain];
[table101 setDataSource:self];
[table101 setDelegate:self];
[table101 setBackgroundColor:[UIColor clearColor]];
[table101 setTag:101];
[table101 setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[scroll1 addSubview:table101];
// #2
UIScrollView *scroll2 = [[UIScrollView alloc] initWithFrame:CGRectMake(viewSize.width, 0, viewSize.width, viewSize.height -149)];
UITableView *table102 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, viewSize.width, viewSize.height -149) style:UITableViewStylePlain];
[table102 setDataSource:self];
[table102 setDelegate:self];
[table102 setBackgroundColor:[UIColor clearColor]];
[table102 setTag:102];
[table102 setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[scroll2 addSubview:table102];
[mainScroll addSubview:scroll1];
[mainScroll addSubview:scroll2];
[mainScroll setContentSize:CGSizeMake(viewSize.width *2, viewSize.height -149)];
}
In TableViewCell I used this:
- (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];
[cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
}
NSMutableArray *mutableArray;
if (tableView.tag == 101) {
mutableArray = self.itemsInTable_Molhaq;
} else if (tableView.tag == 102) {
mutableArray = self.itemsInTable_Ziyarah;
}
NSString *labelString = [[mutableArray objectAtIndex:indexPath.row] objectForKey:#"Title"];
NSInteger indentLevel = [[[mutableArray objectAtIndex:indexPath.row] objectForKey:#"Level"] intValue];
[cell.textLabel setAttributedText:[self attributeString:labelString indentLevel:indentLevel]];
return cell;
}
Everything is working properly, But Xcode shows me this:
Unsupported configuration prototype table cells must have reuse identifiers
What did I miss? Can I get some help?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath*)indexPath
{
static NSString *cellIdentifier = #"wot";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle: someStyle reuseIdentifier: cellIdentifier];
return cell;
}
May be it helps.
It's pretty clear what the error says. You have created a prototype cell in your storyboard and not given it an identifier. Go to the storyboard and look at the properties of the cell. Fill in the identifier.

When Scroll UITableView it overlap the name iOS

When i scroll the tableview gives this type of overlapping
I write the below code in CellForRow AtIndexPath method,I create tableview programatically...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *info = [ASSETHELPER getGroupInfo:_groupArray[indexPath.row]];
UIImageView *view;
if (indexPath.row % 2) {
view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"dark_bg"]];
}else{
view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"light_bg"]];
}
cell.backgroundView=view;
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:25];
[imgView.layer setMasksToBounds:YES];
[imgView.layer setBorderColor:[UIColor whiteColor].CGColor];
[imgView setImage:[info objectForKey:#"thumbnail"]];
[cell.contentView addSubview:imgView];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(80, 20, 200, 30)];
[label setText:[info objectForKey:#"name"]];
[cell.contentView addSubview:label];
return cell;
}
Try this code .You have used dequeueReusableCellWithIdentifier:
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell %d",indexPath.row];
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
You can try with it. Just remove the previous contentView after create a table cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//****add this code*******//
for (UIImageView *v in [cell.contentView subviews])
{
[v removeFromSuperview];
NSLog(#"%# hello \n\n\n\n\n%#",v,[cell.contentView subviews]);
}
for (UILabel *v in [cell.contentView subviews])
{
[v removeFromSuperview];
NSLog(#"%# hello \n\n\n\n\n%#",v,[cell.contentView subviews]);
}/////////
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSDictionary *info = [ASSETHELPER getGroupInfo:_groupArray[indexPath.row]];
UIImageView *view;
if (indexPath.row % 2)
{
view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"dark_bg"]];
}
else
{
view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"light_bg"]];
}
cell.backgroundView=view;
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:25];
[imgView.layer setMasksToBounds:YES];
[imgView.layer setBorderColor:[UIColor whiteColor].CGColor];
[imgView setImage:[info objectForKey:#"thumbnail"]];
[cell.contentView addSubview:imgView];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(80, 20, 200, 30)];
[label setText:[info objectForKey:#"name"]];
[cell.contentView addSubview:label];
return cell;
}

UIButton subview in UITableViewCell doesn't hide as expected

My recent frustration is a UIButton subview in each UITableViewCell of my UITableView which I want to setHidden: according to a specific clause for each indexPath. My code is pretty much the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[self initCell:cell forIndexPath:indexPath];
}
[self updateCell:cell forIndexPath:indexPath];
return cell;
}
and the init and update methods are the following:
- (void)initCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
...
UIButton *btnMy = [UIButton buttonWithType:UIButtonTypeCustom];
btnMy.tag = kButtonMyTag;
[btnMy setFrame:CGRectMake(170, 45, 100, 30)];
[btnMy setBackgroundImage:[UIImage imageNamed:#"btn_image"] forState:UIControlStateNormal];
btnMy.adjustsImageWhenHighlighted = YES;
[btnMy setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnMy.titleLabel.font = [UIFont fontWithName:#"MyFont" size:14];
[btnMy addTarget:self action:#selector(btnMyPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btnMy];
UIImageView *imgViewAccessory = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"table_accessory"]];
cell.accessoryView = imgViewAccessory;
[imgViewAccessory release];
}
- (void)updateCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
UIButton *btnMy = (UIButton *)[cell viewWithTag:kButtonMyTag];
MyObject *object = (MyObject *)[self.dataSource objectAtIndex:indexPath.row];
if(object.meetsCondition)
{
btnMy.hidden = NO;
}
else
{
btnMy.hidden = YES;
}
...
}
The frustrating result is that when scrolling the button shows and hides randomly and not as expected according the if clause in the updateCell method.
Any help would be much appreciated. Thanks in advance!
You should make custom cell and depending upon the condition show and hide the button
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *nib;
static NSString *cellIdentifier= #"cell";
UITableViewCell *theCell = [self.tblView dequeueReusableCellWithIdentifier:cellIdentifier];
if([theCell.contentView subviews]){
for(UIView *view in [theCell.contentView subviews]){
[view removeFromSuperview];
}
}
if(theCell== nil)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"Your custom cell name" owner:self options:nil];
theCell = [nib objectAtIndex:0];
theCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UIButton *btn=(UIButton*)[theCell.contentView viewWithTag:101];
if(yourcondition)
//hide button
else
//show button
}
This will do
Use this code in your CellForRowAtIndexPath Also.
MyObject *object = (MyObject *)[self.dataSource objectAtIndex:indexPath.row];
if(object.meetsCondition) {
btnMy.hidden = NO;
}
else {
btnMy.hidden = YES;
}

how to use indexpath for xml parsing

i am passing data from one view to the other using xml parser.when i click on any row(after the xml is loaded on the view) it should display the entire details accordingly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *titleLabel = nil;
UILabel *priceLabel = nil;
UILabel *titleValueLabel = nil;
UILabel *priceValueLabel = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
titleLabel.text = #"Model:";
priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 50, 20)];
priceLabel.text = #"Price:";
priceValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 20, 50, 20)];
titleValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 1, 320, 20)];
[cell.contentView addSubview:titleLabel];
[cell.contentView addSubview:priceLabel];
[cell.contentView addSubview:titleValueLabel];
[cell.contentView addSubview:priceValueLabel];
}
NSDictionary *tempProduct = [self.listData objectAtIndex:indexPath.row];
NSDictionary *productTitle = [tempProduct valueForKey:TITLE];
NSString *titleValue = [productTitle valueForKey:TEXT];
titleValue = [titleValue stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
titleValueLabel.text = titleValue;
[cell.contentView addSubview:titleValueLabel];
NSDictionary *productPrice = [tempProduct valueForKey:PRICE];
NSString *priceValue = [productPrice valueForKey:TEXT];
priceValue = [priceValue stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
priceValueLabel.text = priceValue;
[cell.contentView addSubview:priceValueLabel];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
and my sample xml data is
<?xml version="1.0" encoding="UTF-8"?>
<scan>
<products>
<title>samsung galaxy</title>
<desc>smart phone</desc>
<price>250$</price>
</products>
<products>
<title>samsung galaxy1</title>
<desc>smart phone1</desc>
<price>251$</price>
</products>
<products>
<title>samsung galaxy2</title>
<desc>smart phone2</desc>
<price>252$</price>
</products>
<products>
<title>samsung galaxy3</title>
<desc>smart phone3</desc>
<price>350$</price>
</products>
</scan>
when i click on the first row,it should display the same data.but it is displaying in the order of the selection.
how to fix this?
you can just passing index like that
- (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];
NSDictionary *tempProduct = [self.listData objectAtIndex:indexPath.row];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
[Name setText:[tempProduct valueForKey:#"TITLE"]];
priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 50, 20)];
[Name setText:[tempProduct valueForKey:#"Price"]];
priceValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 20, 50, 20)];
titleValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 1, 320, 20)];
[cell.contentView addSubview:titleLabel];
[cell.contentView addSubview:priceLabel];
//[cell.contentView addSubview:titleValueLabel];
// [cell.contentView addSubview:priceValueLabel];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
EDIT
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *tempProduct = [self.listData objectAtIndex:indexPath.row];
objAppDelegate.strCatagoryTitle=[tempProduct valueForKey:#"TITLE"];
objAppDelegate.strCatagoryPrice=[tempProduct valueForKey:#"Price"];
//hear objAppDelegate.strCatagoryTitle or objAppDelegate.strCatagoryPrice globle string.
//we can use it at our cntrSecondViewController class using this Delegate object
//objAppDelegate = (cntrAppDelegate1 *) [[UIApplication sharedApplication]delegate];
cntrSecondViewController *cntrinnerService = [[cntrSecondViewController alloc] initWithNibName:#"cntrSecondViewController" bundle:nil];
[self.navigationController pushViewController:cntrinnerService animated:YES];
}

Change label and icon positions of tableview's cell

it' possible to change position oh image and lable of my tableview's cell?
I would not use CustomCell..
I have tried this (for the label)..
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else
return cell;
UIImage *image = nil;
cell.textLabel.text = #"text label";
UILabel *label = cell.textLabel;
CGRect frame = label.frame;
frame.origin.x = 62;
frame.origin.y = 62;
label.frame = frame;
image = [UIImage imageNamed:#"icon.png"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.imageView.image = image;
return cell;
}
Please take one UIview..and give frame size same as cell size...after that add label in that view...also whatever u want to add..add as a subview in that view...than add that view in cell .
All The Best
//table cell contains title,date,image
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell =nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
//tittle label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:contentFrame] ;
titleLabel.numberOfLines = 2;
titleLabel.font = [UIFont boldSystemFontOfSize:12];
titleLabel.text = [currentTweet title];
[cell.contentView addSubview:titleLabel];
//date label
UILabel *dateLabel = [[UILabel alloc] initWithFrame:dateFrame];
dateLabel.font = [UIFont systemFontOfSize:10];
dateLabel.text = [currentTweet dateCreated];
[cell.contentView addSubview:dateLabel];
//image view
CGRect imageFrame = CGRectMake(2, 8, 40,50);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
customImage.image =image;
[cell.contentView addSubview:self.customImage];
return cell;