【问题标题】:Populating a UITableView from arrays从数组填充 UITableView
【发布时间】:2011-05-21 00:35:12
【问题描述】:

我需要以下代码的帮助,我正在使用 hpple 解析 html。 我需要帮助使用这些数据。

-(void) whatever{
NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *titles  = [xpathParser search:@"//h3"]; // get the page title - this is xpath     notation
TFHppleElement *title = [titles objectAtIndex:0];
NSString *myTitles = [title content];

NSArray *articles  = [xpathParser search:@"//h4"]; // get the page article - this is xpath     notation
TFHppleElement *article = [articles objectAtIndex:0];
NSString *myArtical = [article content];

我想从数组“titles”中创建和填充一个表 然后可以点击表格上的项目来加载一个子视图,该子视图应该在同一索引处显示相应的文章?

我想以编程方式或使用 IB 进行此操作

谁能推荐一些示例代码或教程?

谢谢。

【问题讨论】:

    标签: iphone arrays uitableview ios loops


    【解决方案1】:
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [titles count];
    
    }
    - (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.text = [titles objectAtIndex:indexPath.row];
    
            return cell;
    
    }
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    /* here pop up Your subview with corresponding  value  from the array with array index indexpath.row ..*/
    
    }
    

    【讨论】:

    • 太棒了 rajesh,这会为我创建表格还是我需要在界面生成器中创建表格视图?我需要打电话给这些吗?对不起我的无知我是 IOS 开发新手
    • 不,您需要在 didload 中创建一个表格视图。在您的 .h 文件中放入 UITbaleView *yourtableView;并在 viewDidLoad 方法下的 .m 文件中放入 yourtableView =[ [UITableView alloc] initWithFrame:CGRectMake:(put your own coordinates )];然后 yourtableView.delegate = self;然后 [self.view addSubView:yourTableView];和 int 我的答案 UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:MyIdentifier];用 yourTableView 替换“tableView”。克?如果有任何问题,请告诉我?
    • 感谢 rajesh,我使用了您上面的代码并将其与界面生成器中的表格相关联!它完美地工作:)谢谢
    【解决方案2】:

    作为一般方法,您只需将相关控制器类设置为相关 UITableView 的委托,然后实现以下方法:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    不过,我建议阅读 Apple 的 Table View Programming guide - 它不仅能说明简单的代码示例,而且还相当容易理解。

    完成此操作后,如果您仍在使用示例代码,只需下载UITableView Class reference 的“相关示例代码”部分中的项目之一。 (如果您在 Xcode 中的 Apple 文档查看器中执行此操作,它会自动下载等,并会在 Xcode 中为您打开项目。)

    【讨论】:

      【解决方案3】:

      首先,您必须在创建 tableview 的 .h 文件中设置 UITableViewDelegate 和 UITableViewDatasource 并声明一个 NSarray 来存储表值,在 viedidload 函数的 .m 文件中,您需要使用对象初始化数组(arr_name=[ NSArray alloc]initwith Objects:@"one",@"two",nil) 和你要放置这三个方法

      • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

      • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-24
        • 1970-01-01
        • 1970-01-01
        • 2018-09-20
        • 1970-01-01
        • 1970-01-01
        • 2014-02-08
        • 1970-01-01
        相关资源
        最近更新 更多