我正在使用objective-c中的滑出菜单栏。我使用了这个链接https://github.com/arturdev/AMSlideMenu中的一些类。我做了左菜单。现在我遇到的问题是左侧菜单中的项目是Home,AboutUs,Login / Register和Contact。如果用户登录该项登录/注册应该更改为注销并在主页上方,它们应该出现用户配置文件或用户的单元格,直到它保持登录状态。如果用户按下注销项目,则应该注销并将我们带到主屏幕。有谁能够帮我?我从Web服务登录用户数据。左侧菜单栏项目是在静态单元格中生成的。
答案 0 :(得分:0)
在左侧菜单中,您应该编写一个函数,根据用户状态(登录/注销)和注销菜单重新加载表视图为表视图准备数据源。
例如,一个给你一个想法的伪代码:var dataSource: [CustomMenuItemObject] = []
func prepareDataSource()
{
dataSource = []
if(MySessionManager.current.isUserLoggedIn())
{
dataSource.append (createMyProfileMenu())
dataSource.append (createAboutUsMenu())
dataSource.append (createLogoutMenu())
dataSource.append (createContactMenu())
}
else
{
dataSource.append (createHomeMenu())
dataSource.append (createAboutUsMenu())
dataSource.append (createLoginRegisterMenu())
dataSource.append (createContactMenu())
}
myTableView.reloadData()
}
func tableView(....numberOfRows...)
{
return dataSource.count
}
func tableView(....cellForRow...)
{
let cell == tableView.dequeue...
...
...
cell.updateCustomCellUsing(dataSource[indexPath.row])
...
...
return cell
}
func createMyProfileMenu() -> CustomMenuItemModel
{
let item = CustomMenuItemModel()
item.titleText = "Profile"
...
...
return item
}
..
.. // Further menu item creation code
func userDidLogout() // Delegate or NSNotification callback
{
MySessionManager.current.clearUserDataFromSession()
prepareDataSource()
myTableView.reloadData()
}
func userDidLogIn() // Delegate or NSNotification callback
{
MySessionManager.current.setupUserInSession()
prepareDataSource()
myTableView.reloadData()
}
在按下注销时,使用NSNotification或委托在左侧菜单视图控制器中重新加载tableview。
编辑:Objective-C版本
NSMutableArray dataSource = [NSMutableArray new];
-(void)prepareDataSource
{
dataSource = [NSMutableArray new];
if([[MySessionManager current] isUserLoggedIn])
{
[dataSource addObject:[self createMyProfileMenu]];
[dataSource addObject:[self createAboutUsMenu]];
[dataSource addObject:[self createLogoutMenu]];
[dataSource addObject:[self createContactMenu]];
}
else
{
[dataSource addObject:[self createHomeMenu]];
[dataSource addObject:[self createAboutUsMenu]];
[dataSource addObject:[self createLoginRegisterMenu]];
[dataSource addObject:[self createContactMenu]];
}
[myTableView reloadData];
}
-(void)tableView(....numberOfRows...)
{
return dataSource.count;
}
-(void)tableView(....cellForRow...)
{
let cell == tableView.dequeue...
...
...
[cell updateCustomCellUsing(dataSource[indexPath.row])];
...
...
return cell
}
-(CustomMenuItemModel *)createMyProfileMenu
{
CustomMenuItemModel *item = [CustomMenuItemModel new];
item.titleText = "Profile";
...
...
return item
}
..
.. // Further menu item creation code
-(void)userDidLogout // Delegate or NSNotification callback
{
[[MySessionManager current] clearUserDataFromSession];
[self prepareDataSource];
[myTableView reloadData];
}
-(void)userDidLogIn // Delegate or NSNotification callback
{
[[MySessionManager current] setupUserInSession];
[self prepareDataSource];
[myTableView reloadData];
}
答案 1 :(得分:0)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(refreshList)
name:@"refreshList"
object:nil];
if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:@"userlogging"]) {
NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"userlogging"];
if ([login isEqualToString:@"loggedin"]) {
NSArray *titles = @[@"Search Restaurants",@"Change Language",@"Sellers click here",@"Login"];
checklogin = YES;
}
}
else
{
NSArray *titles = @[@"Search Restaurants", @"My Cart", @"Favorite Restaurants", @"Previous Orders",@"Change Language",@"Sellers click here", @"My Profile", @"Logout"];
checklogin = NO;
}
[self.tableView reloadData];