UITableView next and pre 实现

| 2 Comments | No TrackBacks
In Detail ViewController 创建2个button,action如下

[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"];


}


测试,成功

No TrackBacks

TrackBack URL: http://iphone.ipsw.info/mt/mt-tb.cgi/279

2 Comments

Leave a comment