Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uitableview 使用类似indexPath.row的内容从单元格获取URL_Uitableview_Url - Fatal编程技术网

Uitableview 使用类似indexPath.row的内容从单元格获取URL

Uitableview 使用类似indexPath.row的内容从单元格获取URL,uitableview,url,Uitableview,Url,我正在尝试从单元格中获取URL。为此,我使用nsindepath*indepath=[self.tableView indexPathForSelectedRow]然后希望执行类似NSURL*url=self.finalURL[indexPath.row]的操作,但因为indexPath.row仅用于数组,所以这不起作用。是否有一种方法可以实现与indexPath.row相同的功能,但不适用于数组中的对象 以下是我保存url的方式: cell.finalURL = self.finalURL;

我正在尝试从单元格中获取URL。为此,我使用
nsindepath*indepath=[self.tableView indexPathForSelectedRow]
然后希望执行类似NSURL*url=self.finalURL[indexPath.row]的操作,但因为
indexPath.row
仅用于数组,所以这不起作用。是否有一种方法可以实现与
indexPath.row
相同的功能,但不适用于数组中的对象

以下是我保存url的方式:

cell.finalURL = self.finalURL;
单元格没有URL,除非创建单元格的子类并将该属性添加到is。按照惯例,您将拥有一个对象、字符串、字典等数组,这是tableView的数据源

如果我有一个包含三个NSURL的数组,称为myArray,其中包含google、amazon和bing,我想显示三个单元格,每个单元格的标签与数组中的项目匹配,我将实现以下代码:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // we only want a single section for this example
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // this tells the tableView that you will have as many cells as you have items in the myArray array
    return myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // first we try to reuse a cell (if you don't understand this google it, there's a million resources)
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    // if we were unable to reuse a cell
    if (cell == nil) {
        // we want to create one
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

        // here is where we do ANY code that is generic to every cell, such as setting the font,
        // text color, etc...
        cell.textLabel.textColor = [UIColor redColor];
    }

    // here is where we do ANY code that is unique to each cell - traits that are based on your data source
    // that you want to be different for each cell

    // first get the URL object associated with this row
    NSURL *URL = myArray[indexPath.row];

    // then set the text label's text to the string value of the URL
    cell.textLabel.text = [URL absoluteString];

    // now return this freshly customized cell
    return cell;
}
这与默认tableview代码的其余部分以及数组本身的设置一起,会产生以下结果:

当用户点击某个单元格时,您可以访问数组中的URL,并对其执行如下操作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // first deselect the row so it doesn't stay highlighted
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // get the URL associated with this cell by using indexPath.row as an index on your
    // data source array, if you tapped the first cell, it will give you back the first URL in your array...
    NSURL *selectedURL = myArray[indexPath.row];

    // do something with that URL here...
}

将表视图的数据源想象成一堆小立方体。您可以用一百万种不同的方式创建数据源,但一旦创建了数据源,您基本上就可以将项目放入编号的小隔间中。您的表视图本身是基于这些立方体中的内容创建的,因此要使第一个单元格在第一个立方体中显示,依此类推,稍后当用户从该表视图中选择一个单元格时,表视图所做的只是告诉您所选的立方体编号,而你的工作就是使用这些信息从特定的cubbie中检索数据,并用它做你需要做的事情。希望有帮助

查询表的数据源?我将如何执行此操作。另外,是否有一种方法可以将实体属性中的所有对象添加到数组中,而只将数组添加到表中@Richard@matthew:您可以为实体的属性创建字典,然后可以将该字典作为对象添加到数组中。@matthew您似乎需要仔细阅读委托设计模式,尤其是
UITableViewDataSource
所使用的模式。因此,我拍摄了一段视频,然后将视频的url添加到核心数据中。然后我尝试在每个单元格中添加一个。因此,问题更多的是将核心数据放入阵列中,但最好是在不将核心数据对象放入阵列的情况下这样做。这正是您应该做的,将核心数据存储中的对象提取到阵列中,并将其用作数据源。正如我已经说过的,除非您将UITableViewCell子类化并向单元格添加URL属性,否则您不能只是“向每个单元格添加一个”,这根本不是iOS的工作方式。因此,有一件事是,数据将保存到核心数据,然后我将其转换为URL,那么在创建到阵列后是否应该保存每个URL?