@interface SecondViewController : UIViewController {
NSMutableArray *short_desc;
}
- (IBAction)search:(id)sender;
@end
- (IBAction)search:(id)sender {
UIViewController *artikel = [[UIViewController alloc] init];
artikel.title = @"Artikel";
artikel.view = resultView;
BOOL pushViewController = [self xmlRequest];
if (!pushViewController) {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Hinweis" message:@"Für Ihre Suchanfrage wurden keine passenden Einträge in der Datenbank gefunden" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[errorAlert show];
}
else {
[self.navigationController pushViewController:artikel animated:YES];
[tableView reloadData];
}
}
- (BOOL)xmlRequest {
NSString *p1, *p2, *p3, *p4, *p5, *p6;
p1 = [NSString stringWithFormat:@"%d", ([pickerView selectedRowInComponent:0] + 1)];
p2 = [NSString stringWithFormat:@"%d", ([pickerView selectedRowInComponent:1] + 2010)];
switch ([pickerView selectedRowInComponent:2]) {
case 0:
p3 = @"Deutschland";
break;
case 1:
p3 = @"Österreich";
break;
case 2:
p3 = @"Schweiz";
break;
default:
break;
}
p4 = cityField.text;
p5 = branchField.text;
p6 = messeNameField.text;
NSString *url = [NSString stringWithFormat:A4M_XML_SERVICE, p1, p2, p3, p4, p5, p6];
NSString *xmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]];
NSString *xmlStringUTF8 = [NSString stringWithUTF8String:[xmlString UTF8String]];
CXMLDocument *xmlDoc = [[CXMLDocument alloc] initWithXMLString:xmlStringUTF8 options:0 error:nil];
NSString *result;
NSString *error;
short_desc = [NSMutableArray arrayWithCapacity:10];
name = [NSMutableArray arrayWithCapacity:10];
location = [NSMutableArray arrayWithCapacity:10];
startDate = [NSMutableArray arrayWithCapacity:10];
//Check for nil:
NSArray *nodes = [xmlDoc nodesForXPath:@"/xml/items" error:nil];
if (nodes != nil && [nodes count] >= 1) {
for (int i = 0; i < [nodes count]; i++) {
CXMLElement *resultElement = [nodes objectAtIndex:i];
error = [[[[resultElement attributeForName:@"count"] stringValue] copy] autorelease];
}
}
if ([error intValue] == 0) {
return NO;
}
//Short-Description:
nodes = [xmlDoc nodesForXPath:@"/xml/items/item/short_desc" error:nil];
if (nodes != nil && [nodes count] >= 1) {
for (int i = 0; i < [nodes count]; i++) {
CXMLElement *resultElement = [nodes objectAtIndex:i];
result = [[[[resultElement attributeForName:@"data"] stringValue] copy] autorelease];
[short_desc addObject:result];
}
}
//Name:
nodes = [xmlDoc nodesForXPath:@"/xml/items/item/name" error:nil];
if (nodes != nil && [nodes count] >= 1) {
for (int i = 0; i < [nodes count]; i++) {
CXMLElement *resultElement = [nodes objectAtIndex:i];
result = [[[[resultElement attributeForName:@"data"] stringValue] copy] autorelease];
[name addObject:result];
}
}
//Location:
nodes = [xmlDoc nodesForXPath:@"/xml/items/item/location" error:nil];
if (nodes != nil && [nodes count] >= 1) {
for (int i = 0; i < [nodes count]; i++) {
CXMLElement *resultElement = [nodes objectAtIndex:i];
result = [[[[resultElement attributeForName:@"data"] stringValue] copy] autorelease];
[location addObject:result];
}
}
//Date:
nodes = [xmlDoc nodesForXPath:@"/xml/items/item/date" error:nil];
if (nodes != nil && [nodes count] >= 1) {
for (int i = 0; i < [nodes count]; i++) {
CXMLElement *resultElement = [nodes objectAtIndex:i];
result = [[[[resultElement attributeForName:@"start"] stringValue] copy] autorelease];
[startDate addObject:result];
}
}
NSLog([name objectAtIndex:0]);
NSLog([location objectAtIndex:0]);
NSLog([startDate objectAtIndex:0]);
NSLog([short_desc objectAtIndex:0]);
return YES;
}
//TableView:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [name count];
}
#define ASYNC_IMAGE_TAG 9999
#define LABEL_TAG_1 8888
#define LABEL_TAG_2 7777
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSInteger row = [indexPath row];
CGRect tRect3 = CGRectMake(0.0f, 0.0f, 420.0f, 60.0f);
AsyncImageView *asyncImageView = nil;
UILabel *labelDate = nil;
UILabel *labelTitle = nil;
UIImageView *image = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:tRect3 reuseIdentifier:CellIdentifier] autorelease];
[[[cell subviews] objectAtIndex:0] setTag:111];
//Datum-Label:
[labelDate setTextAlignment:UITextAlignmentLeft];
CGRect tRect1 = CGRectMake(65.0f, 3.0f, 215.0f, 15.0f);
labelDate = [[[UILabel alloc] initWithFrame:tRect1] autorelease];
labelDate.tag = LABEL_TAG_1;
//Titel-Label:
[labelTitle setTextAlignment:UITextAlignmentLeft];
CGRect tRect2 = CGRectMake(65.0f, 15.0f, 215.0f, 40.0f);
labelTitle = [[[UILabel alloc] initWithFrame:tRect2] autorelease];
labelTitle.tag = LABEL_TAG_2;
//UITableViewCellAccessoryDisclosureIndicator
CGRect tRect4 = CGRectMake(300.0f, 0.0f, 10.0f, 60.0f);
image = [[[UIImageView alloc] initWithFrame:tRect4] autorelease];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"disclosure" ofType:@"png"]];
[image setImage:img];
[cell.contentView addSubview:labelDate];
[cell.contentView addSubview:labelTitle];
[cell.contentView addSubview:image];
} else {
labelDate = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG_1];
labelTitle = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG_2];
}
UIView *cellView = [cell viewWithTag:111];
if (row % 2) {
[cellView setBackgroundColor:[UIColor whiteColor]];
} else {
[cellView setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1]];
}
NSString *date = [short_desc objectAtIndex:0]; //App crashes at this point
labelDate.text = date;
labelDate.textColor = [UIColor darkGrayColor];
[labelDate setFont:[UIFont systemFontOfSize:12.0f]];
[labelDate setBackgroundColor:[UIColor clearColor]];
//labelTitle.text = [name objectAtIndex:0];
labelTitle.lineBreakMode = UILineBreakModeWordWrap;
labelTitle.numberOfLines = 0;
labelTitle.adjustsFontSizeToFitWidth = YES;
[labelTitle setTextAlignment:UITextAlignmentLeft];
[labelTitle setFont: [UIFont systemFontOfSize:16.0f]];
[labelTitle setBackgroundColor: [UIColor clearColor]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 61.0f;
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
// Navigatoin logic
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigatoin logic
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}