UITableView / UITableView单元索引问题

时间:2012-08-18 21:24:06

标签: ios uitableview if-statement

我正在为我的网页浏览器制作一个书签页面,问题是每次我添加一个新对象时,我必须设置属性,因为我创建了一个自定义单元格(我必须设置两个标签的文本)所以我需要一种方法来只编辑新添加的对象...我熟悉索引但不能提出任何解决这个问题的方法...例如当我将第一页书签好时,但是一旦我书签2页面2个单元格是完全相同的...任何想法?

Heres My Code:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell*)
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
    cell = (CustomCell *) [nib objectAtIndex:0];

}

// Set up the cell...

     NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSString *currentURL = webView.request.URL.absoluteString;
      cell.websiteTitle.text = theTitle;
cell.websiteURL.text = currentURL;
universalURL = currentURL;




return cell;
}

设置单元格时,我需要指向最新的单元格!

提前感谢您!

1 个答案:

答案 0 :(得分:1)

你的方法无法奏效。您不能将单元格用作标题和URL的存储,因为单元格重用(因为它设计不好)。表视图仅为可见行分配单元格,并在滚动表视图时重复使用不同行的单元格。

相反,您应该将标题和网址存储在单独的数据源中,例如NSMutableArray *bookmarks,其中数组中的每个项目都是带有“标题”的NSDictionary, “URL”键。

要向表格添加书签,只需在数组中附加一个新条目,然后在表格视图上调用reloadData

tableView:cellForRowAtIndexPath:方法可以使用行号bookmarks的{​​{1}}数组来填充单元格的所有元素。