CdB*_*CdB 4 height xcode objective-c cell uitableview
我正在尝试在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;
Run Code Online (Sandbox Code Playgroud)
.
//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;
}
Run Code Online (Sandbox Code Playgroud)
有线索吗?[编辑:更改了int storyIndex = indexPath.row;]
Mat*_*uch 11
这是人们对表格单元重用的常见问题.
该行尝试重用一个单元格.这意味着如果单元格0移出屏幕,它将被重用为单元格5:
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
Run Code Online (Sandbox Code Playgroud)
如果一个单元格无法重用,那么您正在创建一个新单元格:
if (cell == nil){
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
Run Code Online (Sandbox Code Playgroud)
在下一行是您的问题,只有在无法重复使用单元格时才设置单元格.这发生了5次(对于表变得可见时可见的单元格).
但是,您的桌子之后想要显示的所有单元格都将重新使用已经包含内容的单元格.
// Set up the cell
/*...*/
Run Code Online (Sandbox Code Playgroud)
但别担心 这很容易解决.您必须将单元的创建与其配置分开.只需转移一些代码,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil){
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
// whatever happened before. You have a valid cell at this point.
// 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;
}
Run Code Online (Sandbox Code Playgroud)
编辑:也许我下次应该读这个问题.但我想我仍然100%正确.
如果我删除tableView:heightForRowAtIndexPath:一切都很好(除了没有我想要的单元格大小)
我认为这是巧合.你有多少细胞?我想大约7或8?一切都很好,因为你的所有细胞都是同时可见的.因此,不需要重复使用单元格,它们都具有应具有的内容.
| 归档时间: |
|
| 查看次数: |
12541 次 |
| 最近记录: |