从另一个表重新加载表视图中的数据

时间:2015-02-13 10:49:31

标签: ios objective-c uitableview reloaddata

我有两张桌子。表1是主表。当我选择行表格2时,我想更改表格1的内容。

这就是我的尝试,但没有效果。我可以看到我的数组(故事)正在改变,但表格没有重新加载。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    if (tableView.tag==0)
    {
        static NSString *CellIdentifier1 = @"Cell1";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
        }

        int storyIndex = [indexPath indexAtPosition: [indexPath length]];



        cell.textLabel.text =[[stories objectAtIndex: indexPath.row] objectForKey: @"title"];
        cell.textLabel.font=cell.textLabel.font = [UIFont systemFontOfSize:14]; //Change this value to adjust size

        NSString *uppercase = [cell.textLabel.text capitalizedString];


        cell.textLabel.text =  uppercase;
        return cell;
    }
    else
    {
        static NSString *CellIdentifier2 = @"Cell2";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
        }

        cell.textLabel.text = [categoryArray objectAtIndex:indexPath.row];
        return cell;
    }
}


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (!tableView.tag==0) {
        title.text=[categoryArray objectAtIndex:indexPath.row];
        [stories removeAllObjects];
        NSString * path =[feeds objectAtIndex:indexPath.row];
        [self parseXMLFileAtURL:path];
        [tableView reloadData];



    }

}




- (void)parseXMLFileAtURL:(NSString *)URL
{
    //stories = [[NSMutableArray alloc] init];

    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];

    if (!tableView.tag==0) {

        [tableView reloadData];
    }


}

3 个答案:

答案 0 :(得分:0)

获取数据后重新加载表视图。参考tableview1并使用该引用调用重新加载数据。

我想当你在你的代码中调用[tableView reloadData]时,它正在重新加载第二个tableview而不是第一个tableview。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {

   if (!tableView.tag==0) {
   title.text=[categoryArray objectAtIndex:indexPath.row];
   [stories removeAllObjects];
   NSString * path =[feeds objectAtIndex:indexPath.row];
   [self parseXMLFileAtURL:path];
    //[tableView1ref reloadData]; or using below code to identify the first tableview with tag and then call reload Data

  }
 }

- (void)parseXMLFileAtURL:(NSString *)URL
{
    //stories = [[NSMutableArray alloc] init];

    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];

      for (UITableView *tableView in self.view.subviews) {
        if (tableView.tag==0) {
        [tableView reloadData];
        }
       }

/*
    if (!tableView.tag==0) {

        [tableView reloadData];
    }*/


}

答案 1 :(得分:0)

例如,如果您想在点击tableTwo的单元格时重新加载tableOne,请尝试按照以下代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     if ([tableView isEqual:tableTwo]) 
    {
        [reload tableOne];
    }
}

答案 2 :(得分:0)

我这样做了:

 - (void)parserDidEndDocument:(NSXMLParser *)parser {

        NSLog(@"all done!");
        NSLog(@"stories array has %d items", [stories count]);
        if (tableView.tag==0) {
            [tableView reloadData];

        }
}
相关问题