[preBtn addTarget:self action:@selector(pre:) forControlEvents:UIControlEventTouchDown];
[nextBtn addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchDown];
-(void)pre:(id)sender
{
//if ([controller preItem] != nil) {
[controller preItem:self];
[webView loadHTMLString:self.content baseURL:[NSURL URLWithString:@""]];
//[webView reload];
//}
}
-(void)next:(id)sender
{
//if ([controller nextItem] != nil) {
[controller nextItem:self];
[webView loadHTMLString:self.content baseURL:[NSURL URLWithString:@""]];
//[webView reload];
//}
}
对UITableView 使用category 增加一个方法,用来修改_selectedIndexPaths
@implementation UITableView (section)
-(void)setSelectRow:(NSUInteger)row section:(NSUInteger)asection
{
NSLog(@"row: %d: asection: %d",row,asection);
NSLog(@"selectedIndexPaths: %@",_selectedIndexPaths);
[_selectedIndexPaths removeLastObject];
[_selectedIndexPaths addObject:[NSIndexPath indexPathForRow:row inSection:asection]];
NSLog(@"selectedIndexPaths: %@",_selectedIndexPaths);
}
@end
UITableViewController 实行下面的方法
-(void)preItem:(NewsReaderViewController*)controller
{
NSIndexPath *selectedRow = [self.tv indexPathForSelectedRow];
NSUInteger row = selectedRow.row;
NSUInteger section = selectedRow.section;
--row;
if(row >= [self.tv numberOfRowsInSection:selectedRow.section])
{
row = 0;
--section;
if(section >= [self.tv numberOfSections])
{
row = 0;
section = 0;
}
}
NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
[self.tv setSelectRow:row section:section];
NSManagedObject *selectedObject = [fetchedResultsController objectAtIndexPath:selectIndexPath];
controller.content = [selectedObject valueForKey:@"content"];
controller.titleLabel.text= [selectedObject valueForKey:@"title"];
}
-(void)nextItem:(NewsReaderViewController*)controller
{
NSIndexPath *selectedRow = [self.tv indexPathForSelectedRow];
NSUInteger row = selectedRow.row;
NSUInteger section = selectedRow.section;
++row;
if(row >= [self.tv numberOfRowsInSection:selectedRow.section])
{
row = 0;
++section;
if(section >= [self.tv numberOfSections])
{
row = 0;
section = 0;
}
}
[self.tv setSelectRow:row section:section];
NSManagedObject *selectedObject = [fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
controller.content = [selectedObject valueForKey:@"content"];
controller.titleLabel.text= [selectedObject valueForKey:@"title"];
}
测试,成功

模拟器上面没问题, Device build 不通过
错误如下,伤心中
Undefined symbols:
"_OBJC_IVAR_$_UITableView._selectedIndexPaths", referenced from:
_OBJC_IVAR_$_UITableView._selectedIndexPaths$non_lazy_ptr in UITableView.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
改方法吧,增加了一个NSIndexPath,解决了
-(void)nextItem:(NewsReaderViewController*)controller selectRowAtIndexPath:(NSIndexPath *)indexPath