Objective c Can';无法在UITableView的第一行上加载数据
我正在尝试使用Objective c Can';无法在UITableView的第一行上加载数据,objective-c,parsing,uitableview,Objective C,Parsing,Uitableview,我正在尝试使用HTMLParser(由Ben Reeves编写)解析HTML数据,并在UITableView上显示结果。由于某些原因,我只能在tableView的最后一行显示结果。下面是代码片段: - (void)requestFinished:(ASIHTTPRequest *)request { NSData *responseData = [request responseData]; NSError *error = [request error]; HTM
HTMLParser
(由Ben Reeves编写)解析HTML数据,并在UITableView上显示结果。由于某些原因,我只能在tableView的最后一行显示结果。下面是代码片段:
- (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;
}
我在哪里犯错
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];
}
您正在创建与arrayNodes阵列具有相同帧大小和位置的傻瓜形阵列
然后在[cell.contentView addSubview:doubabel]中;它将向您显示更新标签时使用的最后一个值。
把那个傻瓜从for循环中拿出来
在您的cellForRowAtIndexPath中:
HTMLNode* arrayNode = [arrayNodes objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:@"%@",[arrayNode allContents]];
您需要在cellForRowAtindexPath方法中创建傻瓜标签。现在只创建一个傻瓜标签,它依次应用于每个单元格,然后设置为下一个单元格,将其放置在最后一个单元格上。因此,你应该这样做: -
我收到EXC_BAD_ACCESS消息。好的,整理好了。谢谢你的帮助。:)我收到EXC\u BAD\u访问消息:(
(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;
}