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];
}
Related
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 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;
}
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;
I'm trying to parse an HTML Data using HTMLParser (by Ben Reeves) and display results on UITableView. For some reasons I'm able to show up results only on last row of the tableView. Here's the code snippet:
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
NSError *error = [request error];
HTMLParser *parser = [[HTMLParser alloc] initWithData:responseData error:&error];
HTMLNode *bodyNode = [parser body];
arrayNodes = [bodyNode findChildrenWithAttribute:#"class" matchingName:#"foo" allowPartial:NO];
for (HTMLNode *arrayNode in arrayNodes) {
NSString *footitle = [arrayNode allContents];
NSLog(#"%#", footitle);
fooLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 30)];
fooLabel.text = (#"%#", footitle);
fooLabel.textColor = [UIColor blackColor];
}
[self.fooTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrayNodes count];
}
- (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.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
// Configure the cell.
// [self.arrayNodes objectAtIndex:indexPath.row];
[cell.contentView addSubview:fooLabel];
return cell;
}
Where am I making the mistake?
for (HTMLNode *arrayNode in arrayNodes) {
NSString *footitle = [arrayNode allContents];
NSLog(#"%#", footitle);
fooLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 30)];
fooLabel.text = (#"%#", footitle);
fooLabel.textColor = [UIColor blackColor];
}
You are creating fooLabel's with same Frame size and location as many as arrayNodes array.
then in [cell.contentView addSubview:fooLabel]; it is showing you the last value that label is updated with.
take out that fooLabel from for loop.
in your cellForRowAtIndexPath:
HTMLNode* arrayNode = [arrayNodes objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[arrayNode allContents]];
You need to create the foolabel in your cellForRowAtindexPath method. Right now only one foolabel is created, and it is applied to each cell in turn and then set to the next cell, leaving it placed on the last cell. So instead you should do 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.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
// Configure the cell.
HTMLNode *arrayNode = [arrayNodes objectAtIndex:indexPath.row];
NSString *footitle = [arrayNode allContents];
UILabel *fooLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 30)];
fooLabel.text = (#"%#", footitle);
fooLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:fooLabel];
[fooLabel release];
return cell;
}
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];
}