Why UITableview data are overlapping? - ios

Why tableview data are overlapping? i have attached my code.
If i explain my code again then
xml data are in records array which are displaying in tableview.
First time when program execute then data are in right place but when i start scroll vertically the data start to display in various cell.suppose first cell shows in last cell,second cell data shows in first cell.....so on.
NOTE THAT: For overlapping problem i also used switch and viewwithtag but still now tableview data start to display in wrong cell when i start scrolling on UITableview.
// Customize the appearance of table view cells.
- (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];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSLog(#"%d",indexPath.section);
switch (indexPath.section) {
case 0:
order_cellView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, cell.frame.size.width,cell.frame.size.height) ];
order_cellView.backgroundColor = [UIColor redColor];
order_cellView.tag =1000;
NSLog(#"index path 0");
CGRect frame;
frame.origin.x =0;
frame.origin.y = 10;
frame.size.height = 30;
frame.size.width = 230;
CGRect valueFrame;
valueFrame.origin.x =237;
valueFrame.origin.y = 5;
valueFrame.size.height = 30;
valueFrame.size.width = 80;
for(int m=0;m<numberOfProduct;m++){
NSLog(#"for loop here");
productLabel = [[UILabel alloc] initWithFrame:frame];
productLabel.tag = m;
productLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:13];
productLabel.backgroundColor = [UIColor clearColor];
productLabel.lineBreakMode = UILineBreakModeCharacterWrap;
productLabel.numberOfLines = 2;
productLabel.textAlignment = UITextAlignmentCenter;
[order_cellView addSubview:productLabel];
productLabel.text = [NSString stringWithFormat:#"%# x %# x %#",[[products objectAtIndex:m] objectAtIndex:1],[[products objectAtIndex:m] objectAtIndex:2],[[products objectAtIndex:m] objectAtIndex:3]];
frame.origin.y += 45;
productValueLabel = [[UILabel alloc] initWithFrame:valueFrame];
productValueLabel.tag = m+2;
productValueLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:13];
productValueLabel.backgroundColor = [UIColor clearColor];
productValueLabel.lineBreakMode = UILineBreakModeCharacterWrap;
productValueLabel.numberOfLines = 2;
productValueLabel.textAlignment = UITextAlignmentCenter;
productValueLabel.text = [NSString stringWithFormat:#"%#",[[products objectAtIndex:m] objectAtIndex:5]];
[order_cellView addSubview:productValueLabel];
valueFrame.origin.y += 45;
}
[cell.contentView addSubview:order_cellView];
break;
case 1:
email_cellView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, cell.frame.size.width,cell.frame.size.height) ];
email_cellView.backgroundColor = [UIColor clearColor];
email_cellView.tag =1001;
emailAdressLabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 0, cell.frame.size.width, cell.frame.size.height)];
emailAdressLabel.tag = 100;
emailAdressLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:13];
emailAdressLabel.backgroundColor = [UIColor clearColor];
emailAdressLabel.lineBreakMode = UILineBreakModeCharacterWrap;
emailAdressLabel.numberOfLines = 2;
emailAdressLabel.textAlignment = UITextAlignmentLeft;
emailAdressLabel.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:6]];
[email_cellView addSubview:emailAdressLabel];
[cell.contentView addSubview:email_cellView];
break;
case 2:
phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, cell.frame.size.width, cell.frame.size.height)];
phoneNumberLabel.tag = 101;
phoneNumberLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:13];
phoneNumberLabel.backgroundColor = [UIColor clearColor];
phoneNumberLabel.lineBreakMode = UILineBreakModeCharacterWrap;
phoneNumberLabel.numberOfLines = 1;
phoneNumberLabel.textAlignment = UITextAlignmentLeft;
phoneNumberLabel.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:7]];
[cell.contentView addSubview:phoneNumberLabel];
break;
case 3:
billingDetails = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 160, cell.frame.size.height)];
billingDetails.tag = 102;
billingDetails.font = [UIFont fontWithName:#"Arial-BoldMT" size:13];
billingDetails.backgroundColor = [UIColor clearColor];
billingDetails.lineBreakMode = UILineBreakModeCharacterWrap;
billingDetails.numberOfLines = 4;
billingDetails.textAlignment = UITextAlignmentLeft;
billingDetails.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:4]];
[cell.contentView addSubview:billingDetails];
break;
case 4:
shippingDetails = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 160, cell.frame.size.height)];
shippingDetails.tag = 103;
shippingDetails.font = [UIFont fontWithName:#"Arial-BoldMT" size:13];
shippingDetails.backgroundColor = [UIColor clearColor];
shippingDetails.lineBreakMode = UILineBreakModeCharacterWrap;
shippingDetails.numberOfLines = 4;
shippingDetails.textAlignment = UITextAlignmentLeft;
shippingDetails.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:5]];
[cell.contentView addSubview:shippingDetails];
break;
case 5:
orderStatus = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, cell.frame.size.width, cell.frame.size.height)];
orderStatus.tag = 104;
orderStatus.font = [UIFont fontWithName:#"Arial-BoldMT" size:13];
orderStatus.backgroundColor = [UIColor clearColor];
orderStatus.lineBreakMode = UILineBreakModeCharacterWrap;
orderStatus.numberOfLines = 1;
orderStatus.textAlignment = UITextAlignmentLeft;
orderStatus.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:19]];
[cell.contentView addSubview:orderStatus];
break;
case 6:
deleteOrder = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, cell.frame.size.width, cell.frame.size.height)];
deleteOrder.tag = 110;
deleteOrder.font = [UIFont fontWithName:#"Arial-BoldMT" size:13];
deleteOrder.backgroundColor = [UIColor clearColor];
deleteOrder.lineBreakMode = UILineBreakModeCharacterWrap;
deleteOrder.numberOfLines = 1;
deleteOrder.textAlignment = UITextAlignmentLeft;
deleteOrder.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:19]];
[cell.contentView addSubview:deleteOrder];
break;
default:
break;
}
}
switch (indexPath.section) {
case 0:
order_cellView = (UIView *)[cell.contentView viewWithTag:1000];
for(int m=0;m<numberOfProduct;m++){
productLabel = (UILabel *)[order_cellView viewWithTag:m];
productValueLabel = (UILabel *)[order_cellView viewWithTag:m+2];
}
if(indexPath.section == 0){
for(int m=0;m<numberOfProduct;m++){
productLabel.text = [NSString stringWithFormat:#"%# x %# x %#",[[products objectAtIndex:m] objectAtIndex:1],[[products objectAtIndex:m] objectAtIndex:2],[[products objectAtIndex:m] objectAtIndex:3]];
productValueLabel.text = [NSString stringWithFormat:#"%#",[[products objectAtIndex:m] objectAtIndex:5]];
}
}
break;
case 1:
email_cellView = (UIView *)[cell.contentView viewWithTag:1001];
emailAdressLabel = (UILabel *)[email_cellView viewWithTag:100];
emailAdressLabel.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:6]];
break;
case 2:
phoneNumberLabel = (UILabel *)[cell.contentView viewWithTag:101];
phoneNumberLabel.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:7]];
break;
case 3:
billingDetails = (UILabel *)[cell.contentView viewWithTag:102];
billingDetails.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:4]];
break;
case 4:
shippingDetails = (UILabel *)[cell.contentView viewWithTag:103];
shippingDetails.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:5]];
break;
case 5:
orderStatus = (UILabel *)[cell.contentView viewWithTag:104];
orderStatus.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:19]];
break;
case 6:
deleteOrder = (UILabel *)[cell.contentView viewWithTag:110];
deleteOrder.text = [NSString stringWithFormat:#"%#",[[records objectAtIndex:0] objectAtIndex:19]];
break;
default:
break;
}
return cell;
}

I think your main problem is that you have seven different "types" of cell but only one "reuse identifier." This means that an order cell could be later reused as a delete order cell. However, when reused, the cell won't necessarily have all the elements it needs to display fully.
For example, email cell has a tag of 1001 and delete cell has a tag of 110, but not vice-versa.

just needs to give different identifier for each cell you are declaring so if you will do that they will identify the cell and data will not merge on each other
and one more sol is to give each cell subview tag and view the cell with subviews tag..

replace CellIdentifier with nil-
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
It is because you are reusing the cell identifier so data is overlapping

Related

How to Create multiple Tags inside UITableViewCell and set action to each tags

I'm creating Tags inside _ParentContentView subView in UITableView Cell. following code create all tags manually and might increase memory for each tags and it's subViews
i like to have Action for each tag
i like to have all tags inside tableivewCell with out scrolling insdie
table view cell. mean tableviewCell should increase/decrease the
content size automatically.
SpecialityCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int xPosition = 0;
int yPosition = 0;
for (NSInteger buttonCount = 0; buttonCount<=dummyString.count-1; buttonCount++)
{
UIView *parentView = [[UIView alloc]init];
parentView.frame = CGRectMake(10, 0, 150, 40);
[parentView setFrame:CGRectMake(xPosition+10, yPosition,100, 25)];
xPosition = xPosition+130;
parentView.backgroundColor = [UIColor blackColor];
parentView.alpha = 0.5;
parentView.layer.cornerRadius = 15;
UIImageView *childView =[[UIImageView alloc]init];
childView.frame = CGRectMake(5, (parentView.frame.size.height-20)/2, 20, 20);
childView.alpha = 1.0;
childView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.png",[appDelegate.topinImageArray objectAtIndex:3]]];
childView.layer.cornerRadius = 10;
[parentView addSubview:childView];
UILabel *nameLabel = [[UILabel alloc] init];
nameLabel.frame = CGRectMake(childView.frame.size.width+10, (parentView.frame.size.height-30)/2, 80, 30);
nameLabel.text = [NSString stringWithFormat:#"%#", [dummyString objectAtIndex:buttonCount]];
nameLabel.textColor = [UIColor whiteColor];
[parentView addSubview:nameLabel];
[cell._ParantContentView addSubview:parentView];
if((buttonCount+1)%3==0)//4 means how many buttons you need for a row
{
yPosition+= 50;
xPosition= 10;
}
}
like this you can do.
label1 = (UILabel*)[cell.contentView viewWithTag:401];
CGFloat widthLabelHeader = CGRectGetWidth(orderTable.frame)/7;
if(!label1)
{
label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(0, 0, widthLabelHeader, 60);
label1.layer.borderWidth = 0.5;
label1.tag = 401;
label1.font = [UIFont fontWithName:#"LaoSangamMN" size:14];
[cell.contentView addSubview:label1];
}
label1.textAlignment = NSTextAlignmentCenter;
label2 = (UILabel*)[cell.contentView viewWithTag:402];
if(!label2)
{
label2 = [[UILabel alloc]init];
label2.frame = CGRectMake(CGRectGetMaxX(label1.frame), 0, widthLabelHeader, 60);
label2.layer.borderWidth = 0.5;
label2.tag = 402;
label2.font = [UIFont fontWithName:#"LaoSangamMN" size:14];
[cell.contentView addSubview:label2];
}
label2.textAlignment = NSTextAlignmentCenter;
label3 = (UILabel*)[cell.contentView viewWithTag:403];
if(!label3)
{
label3 = [[UILabel alloc]init];
label3.frame = CGRectMake(CGRectGetMaxX(label2.frame), 0, widthLabelHeader, 60);
label3.layer.borderWidth = 0.5;
label3.tag = 403;
label3.font = [UIFont fontWithName:#"LaoSangamMN" size:14];
[cell.contentView addSubview:label3];
}
label3.textAlignment = NSTextAlignmentCenter;
label4 = (UILabel*)[cell.contentView viewWithTag:404];
if(!label4)
{
label4 = [[UILabel alloc]init];
label4.frame = CGRectMake(CGRectGetMaxX(label3.frame), 0, widthLabelHeader, 60);
label4.layer.borderWidth = 0.5;
label4.tag = 404;
label4.font = [UIFont fontWithName:#"LaoSangamMN" size:14];
label4.numberOfLines = 4;
[cell.contentView addSubview:label4];
}
label4.textAlignment = NSTextAlignmentCenter;
label5 = (UILabel*)[cell.contentView viewWithTag:405];
if(!label5)
{
label5 = [[UILabel alloc]init];
label5.frame = CGRectMake(CGRectGetMaxX(label4.frame), 0, widthLabelHeader, 60);
label5.layer.borderWidth = 0.5;
label5.tag = 405;
label5.font = [UIFont fontWithName:#"LaoSangamMN" size:14];
[cell.contentView addSubview:label5];
}
label5.textAlignment = NSTextAlignmentCenter;
label6 = (UILabel*)[cell.contentView viewWithTag:406];
if(!label6)
{
label6 = [[UILabel alloc]init];
label6.frame = CGRectMake(CGRectGetMaxX(label5.frame), 0, widthLabelHeader, 60);
label6.layer.borderWidth = 0.5;
label6.tag = 406;
label6.font = [UIFont fontWithName:#"LaoSangamMN" size:14];
[cell.contentView addSubview:label6];
}
label6.textAlignment = NSTextAlignmentCenter;
UIButton *editBtn = (UIButton*)[cell.contentView viewWithTag:407];
if(!editBtn)
{
editBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
editBtn.frame = CGRectMake(CGRectGetMaxX(label6.frame), 0, widthLabelHeader, 60);
editBtn.layer.borderWidth = 2.5;
[editBtn addTarget:self action:#selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
[editBtn setTintColor:[UIColor blackColor]];
editBtn.tag = 407;
[cell.contentView addSubview:editBtn];
UIImageView *editimg = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(editBtn.frame),0, CGRectGetWidth(label6.frame), 60)];
[editimg setImage:[UIImage imageNamed:#"edit-01.png"]];
[cell.contentView addSubview:editimg];
}
dict1 = [arrayFiltered objectAtIndex:indexPath.row];
if( dict1)
{
NSString *strName = [dict1 objectForKey:#"i_name"];
label1.text = #"";
label1.text = strName;
label1.backgroundColor = [UIColor whiteColor];
label1.numberOfLines = 2;
label2.text = #"";
label2.text = [dict1 objectForKey:#"i_code" ];
label2.backgroundColor = [UIColor whiteColor];
label2.numberOfLines = 2;
NSString *strCategory = [dict1 objectForKey:#"i_category"];
NSArray *arrValuesTempCat = [strCategory componentsSeparatedByString:#","];
NSMutableArray *arrKeyTempCat = [NSMutableArray new];
for (int i = 0; i < arrValuesTempCat.count; i++)
{
NSString *strValue = [arrValuesTempCat objectAtIndex:i];
if ([[categoryDict allKeys] containsObject:strValue]) {
NSString *str = [categoryDict objectForKey:strValue];
if (str.length > 0)
{
[arrKeyTempCat addObject:str];
}
}
}
if ([arrKeyTempCat containsObject:#""])
{
[arrKeyTempCat removeObject:#""];
}
if ([arrKeyTempCat containsObject:#" "])
{
[arrKeyTempCat removeObject:#" "];
}
NSString *strCategoryKeysParam = nil;
if (arrKeyTempCat.count == 1)
{
strCategoryKeysParam = [arrKeyTempCat objectAtIndex:0];
}
else
{
strCategoryKeysParam = [arrKeyTempCat componentsJoinedByString:#","];
}
label3.text= #"";
label3.text = strCategoryKeysParam;
label3.backgroundColor = [UIColor whiteColor];
label3.numberOfLines = 2;
NSString *strElements = [dict1 objectForKey:#"i_elements"];
NSArray *arrKeysTemp = [strElements componentsSeparatedByString:#","];
NSMutableArray *arrValuesTemp = [NSMutableArray new];
for (int i = 0; i < arrKeysTemp.count; i++)
{
NSString *strKey = [arrKeysTemp objectAtIndex:i];
if ([[elementsDict allKeys] containsObject:strKey]) {
NSString *strValue = [elementsDict objectForKey:strKey];
[arrValuesTemp addObject:strValue];
}
}
NSString *strValues = [arrValuesTemp componentsJoinedByString:#","];
label4.text = #"";
label4.text = strValues;
label4.backgroundColor = [UIColor whiteColor];
label5.text = #"";
label5.text = [dict1 objectForKey:#"i_quantity_produced" ];
label5.backgroundColor = [UIColor whiteColor];
label6.text = #"";
label6.text = [dict1 objectForKey:#"active_status" ];
label6.backgroundColor = [UIColor whiteColor];
[editBtn setTitle:#"" forState:UIControlStateNormal];
NSString *idStr = [NSString stringWithFormat:#"%d",[[dict1 objectForKey:#"i_id"] intValue]];
NSLog(#"ids : %#, dict : %#",idStr, dict1);
[editBtn addTarget:self action:#selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
editBtn.accessibilityIdentifier = idStr;
NSString *strID = editBtn.accessibilityIdentifier;
NSLog(#"str ID : %#",strID);
editItemId = [[dict1 objectForKey:#"i_id"]intValue];
NSLog(#"Edited Item : %d",editItemId);
// [appDelegate hideIndicator];
}
}
For set row height according to it's content you should set tableview's two properties
_tableViewOne.estimatedRowHeight = 50; //minimum row height
_tableViewOne.rowHeight = UITableViewAutomaticDimension;
so it will take row height according to it's content.
Don't use :
heightForRowAtIndexPath method when using above code.

Custom subviews on UITableViewCell and scrolling issue

I would like to get some suggesstions about the code shown below. The scrolling is kind of slow on iPhone, but not on simulator. What I am trying to do is to show multiple hours and messages on each row and each row may have different numbers of hours and messages.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// message + hours
static NSString *CellIdentifier = #"Cell1";
// others
static NSString *CellIdentifier1 = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
UILabel *hoursLabel;
UILabel *infoLabel;
UILabel *dayLabel;
switch (indexPath.section) {
case 0:
cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
if ([hoursArray count] > 0 && [infoArray count] > 0) {
harray = [self seperateString:[hoursArray objectAtIndex:indexPath.row]];
iarray = [self seperateString:[infoArray objectAtIndex:indexPath.row]];
// check how many hours in an array
int loop = [harray count];
int currentInfoHeight = 0;
int currentHourHeight = 0;
int labelHeight = 0;
for (int i = 0; i < loop ; i++) {
NSString *Text = [[NSString alloc] initWithFormat:#"%#", [harray objectAtIndex:i]];
NSString *Text1 = [[NSString alloc] initWithFormat:#"%#", [iarray objectAtIndex:i]];
UIFont *cellFont = [UIFont systemFontOfSize:hourfontSize];
UIFont *cellFont1 = [UIFont systemFontOfSize:messageFontSize];
CGSize constraintSize = CGSizeMake(180.0f, MAXFLOAT);
CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGSize labelSize1 = [Text1 sizeWithFont:cellFont1 constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
/* HourLabel */
hoursLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
70.0 + 2.0 * cell1.indentationWidth,
currentHourHeight + gap,
tableView.bounds.size.width - 70.0 - 4.0 * cell1.indentationWidth,
labelSize.height)]
autorelease];
hoursLabel.text = [NSString stringWithFormat:[harray objectAtIndex:i]];
hoursLabel.backgroundColor = [UIColor clearColor];
hoursLabel.textColor = [UIColor blackColor];
// hoursLabel.shadowColor = [UIColor blackColor];
// hoursLabel.shadowOffset = CGSizeMake(0, 1);
hoursLabel.font = [UIFont systemFontOfSize:hourfontSize];
[cell1.contentView addSubview:hoursLabel];
if (![[iarray objectAtIndex:i] isEqualToString:#"-"]) {
/* infoLabel */
infoLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
70.0 + 2.0 * cell1.indentationWidth,
currentInfoHeight + gap + labelSize.height,
tableView.bounds.size.width - 70.0 - 4.0 * cell1.indentationWidth,
labelSize1.height)]
autorelease];
infoLabel.text = [NSString stringWithFormat:[iarray objectAtIndex:i]];
infoLabel.numberOfLines = 0;
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.textColor = [UIColor colorWithRed:51.0/255 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
infoLabel.font = [UIFont systemFontOfSize:messageFontSize];
[cell1.contentView addSubview:infoLabel];
labelHeight = (infoLabel.bounds.size.height);
}
else
{
labelHeight=0;
}
/* store current height of label */
currentHourHeight = (hoursLabel.bounds.size.height) + labelHeight + gap + currentHourHeight;
currentInfoHeight = (hoursLabel.bounds.size.height) + labelHeight + gap + currentInfoHeight;
}
}
/* dayLabel */
dayLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
2.0 * cell1.indentationWidth,
[self tableView:tableView_ heightForRowAtIndexPath:indexPath] / 2.0f - dayFontSize/2 ,
tableView.bounds.size.width -
70.0 - 4.0 * cell1.indentationWidth,
dayFontSize)]
autorelease];
[cell1.contentView addSubview:dayLabel];
/* Configure the properties for the text that are the same on every row */
dayLabel.backgroundColor = [UIColor clearColor];
dayLabel.textColor = [UIColor colorWithRed:207.0/255 green:181.0/255.0 blue:59.0/255.0 alpha:1.0];
dayLabel.font = [UIFont boldSystemFontOfSize:dayFontSize];
/* Draw a line to divide info and message into two sections */
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(79, 0, 1.5, cell1.contentView.bounds.size.height)] autorelease];
lineView.backgroundColor = [self.tableView_ separatorColor];
lineView.autoresizingMask = 0x3f;
[cell1.contentView addSubview:lineView];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"EEE"];
dayLabel.text = [NSString stringWithFormat:[daysArray objectAtIndex:[indexPath row]]];
[cell1 setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell1;
case 1:
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"View information for this location";
cell.textLabel.font = [UIFont systemFontOfSize:16];
cell.textLabel.textAlignment = UITextAlignmentCenter;
return cell;
case 2:
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Show building on campus map";
cell.textLabel.font = [UIFont systemFontOfSize:16];
cell.textLabel.textAlignment = UITextAlignmentCenter;
return cell;
case 3:
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Direction to building";
cell.textLabel.font = [UIFont systemFontOfSize:16];
cell.textLabel.textAlignment = UITextAlignmentCenter;
return cell;
default:
break;
}
return cell;
}
You are allocating a NSDateFormatter for each cell. In my experience NSDateFormatter allocation and configuration are some of the most expensive calls available. They take significant amount of time.
Make that NSDateFormatter an instance variable so you have to allocate and configure it exactly one time.
you are not reusing your cells. If you don't reuse your cells your performance will suffer.
The pattern to reuse is something like this:
.
- (NSDateFormatter *)weekDayDateFormatter {
if (!myWeekDayDateFormatter) {
myWeekDayDateFormatter = [[NSDateFormatter alloc] init];
[myWeekDayDateFormatter setDateFormat:#"EEE"];
}
return myWeekDayDateFormatter;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSInteger secondLabelTag = 1001;
static NSInteger imageViewTag = 1002;
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell = nil;
switch (indexPath.section) {
case 0: {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1];
// allocate subviews and configure properties that never change
UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectZero];
secondLabel.tag = secondLabelTag;
secondLabel.textColor = [UIColor orangeColor];
[cell.contentView addSubview:secondLabel];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
imageView.tag = imageViewTag;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[cell.contentView addSubview:imageView];
}
// Whatever happened before you have a valid cell here
UILabel *secondLabel = (UILabel *)[cell.contentView viewWithTag:secondLabelTag];
UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:imageViewTag];
secondLabel.text = [self.weekDayDateFormatter stringFromDate:[dataSource objectAtIndex:indexPath.row]];
imageView.image = [dataSource objectAtIndex:indexPath.row];
break;
}
case 1: {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
// configure properties that never change between cells
cell.textLabel.textColor = [UIColor greenColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// configure properties that are different between cells
cell.textLabel.text = [dataSource objectAtIndex:indexPath.row];
cell.textLabel.backgroundColor = [dataSource objectAtIndex:indexPath.row];
break;
}
}
The code in the tableView:cellForRowAtIndexPath: parts that are called every time should execute as fast as possible. During scrolling this method is called for every single cell.
I guess, the performance isn't lost here, but in -cellForRowAtIndexPath:
Do you use – prepareForReuse / – dequeueReusableCellWithIdentifier:?

uitableview reloaddata cache?

I'm having some problems with an UITableView and the method reloadData.
I'm refreshing the tableView when clicking over a refresh button with the last info in a data base ordered descendingly so the most recent items should appear in first position.
Every cell in my tableview is being filled with custom labels and uiimageviews.
Well... the thing is that when I press that refresh button and new data is found, the first 5 rows (which are the ones that are being shown in the display) aren't updated but the following rows do. However, if I press over any of the 5 first rows, they call correctly to the tableView:didSelectRowAtIndexPath: method with the new data.
So, the problem is that the tableview's "visual" content of the first 5 rows is not being updated.
Anyone can help me with this?
Thanks!
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [ NSString stringWithFormat: #"%d:%d", [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition:1 ]];
UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero reuseIdentifier: CellIdentifier] autorelease];
// Configure the cell...
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
Activity *activity = [[self.activities valueForKey:[[[self.activities allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text=#"";
//Mostramos el nombre
UILabel *label= [[UILabel alloc] initWithFrame:CGRectMake(60, -5, 200, 34)];
label.text = [activity name];
label.font = [UIFont boldSystemFontOfSize:17];
label.backgroundColor = [UIColor clearColor];
[cell addSubview:label];
[label release];
//Mostramos la imagen
UIImageView *leftImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 3, 50, 50)];
[cell addSubview:leftImage];
UITextView *textView=[[UITextView alloc] initWithFrame:CGRectMake(60, 25, 200, 38)];
textView.editable = NO;
textView.backgroundColor = [UIColor clearColor];
textView.textColor = [UIColor grayColor];
textView.font = [UIFont systemFontOfSize:12.0];
textView.contentInset = UIEdgeInsetsMake(-8,-8,0,0);
textView.userInteractionEnabled=NO;
[cell addSubview:textView];
switch ([activity notType]) {
case 0:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"requestPUSH",#""),[activity name]];
leftImage.image = [UIImage imageNamed:#"ios.png"];
break;
case 1:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPCreatedPushString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"mpNew.png"];
break;
case 2:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPUpdatedPushString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"mpUpdated.png"];
break;
case 3:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPDeletedPushString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"ios.png"];
break;
case 4:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPGuestConfirmationPUSHString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"attend.png"];
break;
case 5:
if ([[activity message] isEqualToString:#"noData"]) {
textView.text = [NSString stringWithFormat:NSLocalizedString(#"ShoutPushString",#""),[activity name]];
}else{
textView.text = [NSString stringWithFormat:NSLocalizedString(#"ShoutPushStringWithData",#""),[activity name], [activity message]];
}
UIImage *contactImage = [UIImage imageWithData:[[activity person] pic]];
if (contactImage!=nil) {
leftImage.image = contactImage;
//redondeamos bordes
CALayer * l = [leftImage layer];
[l setMasksToBounds:YES];
[l setCornerRadius:5.0];
}else{
leftImage.image = [UIImage imageNamed:#"ios.png"];
}
break;
case 6:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"CheckinPushString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"ios.png"];
break;
case 7:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPGuestRejectionPUSHString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"reject.png"];
break;
default:
break;
}
[leftImage release];
[textView release];
//Mostrar fecha
double timestamp = [[activity datetime] doubleValue]/1000;
NSString *dateShow;
NSDate *datetime = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH:mm"];
dateShow=[dateFormat stringFromDate:datetime];
UILabel *label2= [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 70, 20)];
label2.text = dateShow;
label2.textAlignment = UITextAlignmentRight;
label2.font = [UIFont systemFontOfSize:12];
label2.backgroundColor = [UIColor clearColor];
label2.textColor = [UIColor blueColor];
[cell addSubview:label2];
[label2 release];
}
return cell;
}
Hmm, so you give each cell a unique cell identifier. Do you have a reason for that?
Mostly people try to patch their misunderstanding of iOS table views that way ...
When you reload your table this call UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier];
will get you a cell to reuse (read already contains content).
But your code only works for "fresh" cells : if (cell == nil) {
, you're just not prepared for that case ...

Reload UITableview data in cell out of view

[self.countryList reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:6 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone]; // or NO ;)
My problem is that I have a tableview with 10+ items with only 6 cells in view, but when the data is loaded and the table is scrolled cell 7 has no data in it. How can I reload the data in that 1 cell?
The code above is what I am using and it doesn't work!
---EDIT---
if (cell == nil) {
cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier]autorelease];
//[cell setBackgroundColor: [UIColor clearColor]];
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(52.0, 0.0, 200.0, 20.0)] autorelease];
mainLabel.tag = 1;
mainLabel.font = [UIFont systemFontOfSize:14.0];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
mainLabel.opaque = YES;
mainLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:mainLabel];
revenue = [[[UILabel alloc] initWithFrame:CGRectMake(52.0, 20.0, 150.0, 20.0)] autorelease];
revenue.tag = 3;
revenue.font = [UIFont systemFontOfSize:14.0];
revenue.textAlignment = UITextAlignmentLeft;
revenue.textColor = [UIColor blackColor];
revenue.opaque = YES;
revenue.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:revenue];
promos = [[[UILabel alloc] initWithFrame:CGRectMake(252.0, 20.0, 150.0, 20.0)] autorelease];
promos.tag = 4;
promos.font = [UIFont systemFontOfSize:14.0];
promos.textAlignment = UITextAlignmentLeft;
promos.textColor = [UIColor blackColor];
promos.opaque = YES;
promos.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:promos];
updates = [[[UILabel alloc] initWithFrame:CGRectMake(252.0, 0.0, 200.0, 20.0)] autorelease];
updates.tag = 5;
updates.font = [UIFont systemFontOfSize:14.0];
updates.textAlignment = UITextAlignmentLeft;
updates.textColor = [UIColor blackColor];
updates.opaque = YES;
updates.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:updates];
photo = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)] autorelease];
photo.contentMode= UIViewContentModeScaleToFill;
photo.tag = 2;
photo.opaque = YES;
photo.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:photo];
}
else {
cell.clearsContextBeforeDrawing=YES;
mainLabel = (UILabel *)[cell.contentView viewWithTag:1];
photo = (UIImageView *)[cell.contentView viewWithTag:2];
promos=(UILabel *)[cell.contentView viewWithTag:4];
revenue=(UILabel *)[cell.contentView viewWithTag:3];
updates=(UILabel *)[cell.contentView viewWithTag:5];
if(photo.image){
photo.image=nil;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageURL =[NSString stringWithString:[(arrayResults*)[tweets objectAtIndex:indexPath.row] myCountryIcon]];
NSArray *myArray = [imageURL componentsSeparatedByString: #"/"];
NSString *fileName = [NSString stringWithString:[myArray lastObject]];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/AppIcons"];
NSString* path = [dataPath stringByAppendingPathComponent:fileName];
UIImage* image = [UIImage imageWithContentsOfFile:path];
if (!image) {
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
// Create an array with the URL and name.
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:fileName,imageURL, nil ];
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
[arr release];
}
else{
photo.image=image;
}
NSString*promostext=[[NSString alloc] initWithFormat:#"promos:%#",[[(arrayResults*)[tweets objectAtIndex:indexPath.row] mypromos]stringValue] ];
NSString*revenuetext=[[NSString alloc] initWithFormat:#"revenue:%#",[(arrayResults*)[tweets objectAtIndex:indexPath.row] myrevenue] ];
NSString*updatestext=[[NSString alloc] initWithFormat:#"updates:%#",[[(arrayResults*)[tweets objectAtIndex:indexPath.row] myupdates]stringValue] ];
mainLabel.text=[(arrayResults*)[tweets objectAtIndex:indexPath.row] myCountryName];
promos.text=promostext;
revenue.text=revenuetext;
updates.text=updatestext;
[promostext release];
[revenuetext release];
[updatestext release];
}
This is typically a misuse in the recycle/reuse mechanism of cellForRowAtIndexPath:.
Cells are reused when you scroll to avoid allocating too much UITableViewCells. So you need to set the content of the cell in cellForRowAtIndexPath: in both case, either if the cell just have been allocated (cell==nil) or if the cell has been reused (cell!=nil).

What will be the numberOfRowsInSection return type for uitableview when XML data is retrieved?

I have a NSMutableArray named as records and a NSArray.
Now I am retrieving an XML attribute and its value, using TBXML parser and inserting the attribute value in the NSArray.
This NSArray is an object of records.
What will be the numerberOfRowsInSection when I want to display the value of XML data in tableview cell?
This is my data retrieving procedure....
[records addObject:[NSArray arrayWithObjects:
[TBXML textForElement:id],
[TBXML textForElement:productNumber],
[TBXML textForElement:name],
[TBXML textForElement:availableStock],
[TBXML textForElement:image],
[TBXML textForElement:quantityOrderMin],
[TBXML textForElement:dateCreated],
[TBXML textForElement:dateUpdated],nil]];
And my XML data are....
<ProductData HASH="21106941">
<id>1</id>
<productNumber>a91cc0f4c7</productNumber>
<name>Product 1</name>
<seoTitle>product-1</seoTitle>
<viewCount>0</viewCount>
<availableStock>100.0</availableStock>
<lowStock>0.0</lowStock>
<image>5e928bbae358c93caedf6115fa7d178b.jpg</image>
<quantityOrderMax>20.0</quantityOrderMax>
<dateCreated>2011-10-06T16:08:45</dateCreated>
</ProductData>
<ProductData HASH="409555632">
<id>2</id>
<productNumber>d8287e2e51</productNumber>
<name>Product 2</name>
<seoTitle>product-2</seoTitle>
<description>
<p>Lorem ipsum dolor sit amet, consectetue...</p>
</description>
<viewCount>0</viewCount>
<availableStock>100.0</availableStock>
<image>8bbd8dfff3cdd28285d07810a4fe7c32.jpg</image>
<quantityOrderMin>1.0</quantityOrderMin>
<dateCreated>2011-10-06T16:08:45</dateCreated>
</ProductData>
I am being able to retrieve data in UITableviewCell, but it's displaying first two retrieved data of the NSArray because the number of rows are two in records array. but i want to set the numberOfRowsInsection will be according to the total number of attribute value retrieved by NSArray.
I have got the problem and I know the problem is here:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"The number of row in the object array: %d ",[records count]);
return [records count];
}
How can I fix it?
#pragma mark tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int sectionCount = [records count];
NSLog(#"section cout: %d",sectionCount);
return sectionCount;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
cell.accessoryView = imageView;
cell.accessoryType = UITableViewCellSelectionStyleNone;
tableView.separatorColor = [UIColor clearColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
cellView = [[[UIView alloc] initWithFrame:CGRectMake(5,8,290, 120)] autorelease];
cellView.backgroundColor = [UIColor clearColor];
cellView.tag =10;
[cell.contentView addSubview:cellView];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 40, 48, 48)];
imgView.image = [UIImage imageNamed:#"productbox.png"];
imgView.layer.borderColor = [UIColor blackColor].CGColor;
imgView.layer.borderWidth = 2.0;
imgView.tag = 5;
[cellView addSubview:imgView];
CGRect idLabelRect = CGRectMake(65, 0, 190, 18);
idLabel = [[[UILabel alloc] initWithFrame:idLabelRect] autorelease];
idLabel.textAlignment = UITextAlignmentLeft;
idLabel.textColor = [UIColor blackColor];
idLabel.font = [UIFont systemFontOfSize:12];
idLabel.backgroundColor = [UIColor clearColor];
idLabel.layer.borderColor = [UIColor grayColor].CGColor;
idLabel.tag = 0;
CGRect statusRect = CGRectMake(65, 22, 190, 22);
statusLabel = [[[UILabel alloc] initWithFrame:statusRect] autorelease];
statusLabel.textAlignment = UITextAlignmentLeft;
statusLabel.textColor = [UIColor blackColor];
statusLabel.font = [UIFont systemFontOfSize:12];
statusLabel.backgroundColor = [UIColor clearColor];
statusLabel.layer.borderColor = [UIColor grayColor].CGColor;
statusLabel.tag = 1;
CGRect orderDateRect = CGRectMake(65, 48, 190, 22);
orderDate = [[[UILabel alloc] initWithFrame:orderDateRect] autorelease];
orderDate.textAlignment = UITextAlignmentLeft;
orderDate.textColor = [UIColor blackColor];
orderDate.font = [UIFont systemFontOfSize:12];
orderDate.backgroundColor = [UIColor clearColor];
orderDate.layer.borderColor = [UIColor grayColor].CGColor;
orderDate.tag = 2;
CGRect byRect = CGRectMake(65, 75, 190, 22);
byLabel = [[[UILabel alloc] initWithFrame:byRect] autorelease];
byLabel.textAlignment = UITextAlignmentLeft;
byLabel.textColor = [UIColor blackColor];
byLabel.font = [UIFont systemFontOfSize:12];
byLabel.backgroundColor = [UIColor clearColor];
byLabel.layer.borderColor = [UIColor grayColor].CGColor;
byLabel.tag = 3;
CGRect totalRect = CGRectMake(65, 98, 190, 22);
totalLabel = [[[UILabel alloc] initWithFrame:totalRect] autorelease];
totalLabel.textAlignment = UITextAlignmentLeft;
totalLabel.textColor = [UIColor blackColor];
totalLabel.font = [UIFont systemFontOfSize:12];
totalLabel.backgroundColor = [UIColor clearColor];
totalLabel.layer.borderColor = [UIColor grayColor].CGColor;
totalLabel.tag = 4;
[cellView addSubview:idLabel];
[cellView addSubview:statusLabel];
[cellView addSubview:orderDate];
[cellView addSubview:byLabel];
[cellView addSubview:totalLabel];
}
cellView = (UIView *)[cell.contentView viewWithTag:10];
idLabel = (UILabel *)[cellView viewWithTag:0];
statusLabel = (UILabel *)[cellView viewWithTag:1];
orderDate = (UILabel *)[cellView viewWithTag:2];
byLabel = (UILabel *)[cellView viewWithTag:3];
totalLabel = (UILabel *)[cellView viewWithTag:4];
imgView = (UIImageView *)[cellView viewWithTag:5];
idLabel.text = [NSString stringWithFormat:#"Order Id: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:0]];
statusLabel.text = [NSString stringWithFormat:#"Status: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:1]];
orderDate.text = [NSString stringWithFormat:#"Date: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:2]];
byLabel.text =[NSString stringWithFormat:#"By: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:3]];
totalLabel.text =[NSString stringWithFormat:#"Total: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:4]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the selected country
NSString *selectedSection = [ NSString stringWithFormat:#"%#",[[records objectAtIndex:indexPath.section] objectAtIndex:0] ];
dvController = [[OrderDetailsViewController alloc] initWithNibNameAndCurrentTweetUser:#"OrderDetailsViewController" bundle:nil:selectedSection];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 130;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
[v setBackgroundColor:[UIColor blackColor]];
UILabel* hdr = [[[UILabel alloc] initWithFrame:CGRectMake(10,0, tableView.bounds.size.width,20)] autorelease];
hdr.textAlignment = UITextAlignmentLeft;
hdr.font = [UIFont fontWithName:#"Arial-BoldMT" size:12];
hdr.textColor = [UIColor whiteColor];
hdr.backgroundColor = [UIColor blackColor];
[v addSubview:hdr];
hdr.text = [NSString stringWithFormat:#"Order #%#",[[records objectAtIndex:section] objectAtIndex:0]];
return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
There are two ways to approach this problem.
The right way would be to take a look at this TBXML tutorial regarding parsing. This shows you what to do to parse each of the attributes out of each ProductData element entry.
The brute force method (which isn't one that I would do) would be to parse through your "records" array, which looks like this:
#"<id>1</id>",#"<productNumber>a91cc0f4c7</productNumber>",#"<name>Product 1</name>",#"<seoTitle>product-1</seoTitle>",#"<viewCount>0</viewCount>",
#"<availableStock>100.0</availableStock>",#"<lowStock>0.0</lowStock>",#"<image>5e928bbae358c93caedf6115fa7d178b.jpg</image>",
#"<basePrice>10.0</basePrice>",#"<costPrice>0.0</costPrice>",#"<height>1.0</height>",#"<width>1.0</width>",#"<depth>1.0</depth>",#"<weight>2.0</weight>"
and so on via this fast enumeration
for(NSString * elementText in records)
{
NSLog( #"%#", elementText );
// and search for "<" and ">" and strip out the name and the value using
// substring
}

Resources