【发布时间】:2011-07-02 03:26:57
【问题描述】:
我正在尝试在 xcode 中构建一个应用程序,该应用程序 - 除了其他人 - 读取 rss 提要并显示帖子。我是 Objective-c 的新手,有时会觉得有点难。
我对检索到的故事(帖子)使用 NSMutableArray。每个故事都由一个 NSMutableDictionary 表示,其中包含帖子的标题、主题、日期和链接。所有这些都显示在 UIViewController 内的 UITableView 中。 我已经定制了自己的单元格,所以我可以在其中显示多个标签。
我的问题是,如果我使用 tableView:heightForRowAtIndexPath:,前 5 个单元格(适合屏幕)显示正常,但如果向下滚动,下一个单元格似乎与前 5 个单元格具有相同的内容(即单元格 0- 4 显示正常,单元格 5 有单元格 0 的内容,单元格 1 的单元格 6 等)!如果我删除 tableView:heightForRowAtIndexPath: 一切都很好(除了没有我想要的单元格大小)
以下是代码的外观:
// NavigationContentsViewController.h
@interface NavigationContentsViewController :
UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *myTableView;
IBOutlet UITableView * newsTable;
UIActivityIndicatorView * activityIndicator;
CGSize cellSize;
NSXMLParser * rssParser;
NSMutableArray * stories;
NSMutableDictionary * item; // it parses through the document, from top to bottom...
NSString * currentElement;
NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink;
}
@property(nonatomic,retain)NSMutableArray *itemsList;
@property(nonatomic,retain)UITableView *myTableView;
- (void)parseXMLFileAtURL: (NSString *)URL;
.
//NavigationContentsViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cell.
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil){
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
// Set up the cell
int storyIndex = indexPath.row;
//[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
//Story title
//cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
//cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.lTitle.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
cell.lSummary.text = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"];
cell.lDate.text = [[stories objectAtIndex: storyIndex] objectForKey: @"date"];
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedCellItem = [NSString stringWithFormat:@"%d", indexPath.row];
TableViewController *fvController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]];
fvController.selectedCellItem = selectedCellItem;
[self.navigationController pushViewController:fvController animated:YES];
[fvController release];
fvController = nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
有什么线索吗? [编辑:更改 int storyIndex = indexPath.row;]
【问题讨论】:
-
你能解释一下为什么你使用
int storyIndex = [indexPath indexAtPosition:[indexPath length] - 1];而不是int storyIndex = indexPath.row;吗? -
如果涉及部分,它确实会有点复杂......在这种情况下,我建议您为每个部分创建多个 NSMutableArrays(但话又说回来,您可能必须创建一个更大的数组来存储它,或者如果它变得太大,你将不得不直接从磁盘开始加载)。
-
@NR4TR 首先感谢您打扰我的问题!正如所写,这是我第一次处理 obj-c,所以很多时候我可能会使用错误的东西,或者用奇怪的方式做事。只是看了你的推荐,我觉得很傻......无论如何,这并没有改变我的问题:)
-
我从未使用过 indexAtPosition,我不确定它会返回什么,但我相当肯定它会返回当前屏幕上的单元格的索引,这是导致您的问题。如果您的表格视图中没有部分,则只需使用 indexPath.row。
-
@Inspire48 我有点困惑。多个 NSMutableArrays 将如何解决问题?事实上,它不会解决它,我只是改变做事的方式。为什么改变单元格的高度与单元格的内容有关?或者 heightForRowAtIndexPath 不仅仅与单元格的高度有关? (顺便感谢您的回复)
标签: objective-c xcode uitableview height cell