如果用户已登录,UITableView会更改内容(UITableView change content if User is logged in)

编程入门 行业动态 更新时间:2024-10-24 23:20:49
如果用户已登录,UITableView会更改内容(UITableView change content if User is logged in)

我对IOS很新,我对我改变TableView内容的方式感到有些困惑,具体取决于用户是否登录。

我有一个MenuViewController,它是一个带有tableView和delegate / datasource协议的UIViewController。

在MenuViewController.m中

- (void)viewDidLoad { [super viewDidLoad]; self.credentialStore = [[CredentialStore alloc] init]; //ref to the object which defines my session [self.slidingViewController setAnchorRightRevealAmount:280.0f]; }

这是我对单元格文本的获取

- (NSArray *)menuItems { if (// Not logged in) { _menuItems = @[@"foo", @"bar", @"baz"]; return _menuItems; } else { _menuItems = @[@"quux", @"other", @"other"]; return _menuItems; } } - (NSArray *)menuItemsImages { if (// Not logged in) { _menuItemsImages = @[@"foo.png", @"bar.png", @"baz.png"]; return _menuItemsImages; } else { .... return _menuItemsImages; } } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.menuItems.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MenuCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } NSString *menuItem = self.menuItems[indexPath.row]; cell.textLabel.text = menuItem; cell.imageView.image = [UIImage imageNamed:self.menuItemsImages[indexPath.row]]; [cell setBackgroundColor:[UIColor clearColor]]; return cell; }

pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *menuItem = self.menuItems[indexPath.row]; if ([menuItem isEqualToString:@"foo"]) { self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"fooNavigationVC"]; } else if ([menuItem isEqualToString:@"bar"]) { self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"barNavigationController"]; } else if ([menuItem isEqualToString:@"baz"]) { self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"bazNavigationViewController"]; } [self.slidingViewController resetTopViewAnimated:YES]; }

我有点困惑,因为使用该方法,tableview有时不会更新。

据我所知,我现在看到两个解决方案: - 检查用户是否登录ViewWillAppear并更改内容 - 在ViewDidLoad中设置菜单,如果用户登录则向MenuViewController发送通知,[self.tableView reloadData] ;

它也有通过比较字符串来检查按下哪个单元格不是很好。

如果有人可以建议一些更好的想法,如何根据用户的记录改变该菜单的内容将是伟大的。 谢谢

I'm fairly new to IOS and I'm a bit confused about the way I can change my TableView's content depending on whether the user is logged in or not.

I have a MenuViewController which is a UIViewController with a tableView and delegate/datasource protocols.

In MenuViewController.m

- (void)viewDidLoad { [super viewDidLoad]; self.credentialStore = [[CredentialStore alloc] init]; //ref to the object which defines my session [self.slidingViewController setAnchorRightRevealAmount:280.0f]; }

This is my getter for the cell's text

- (NSArray *)menuItems { if (// Not logged in) { _menuItems = @[@"foo", @"bar", @"baz"]; return _menuItems; } else { _menuItems = @[@"quux", @"other", @"other"]; return _menuItems; } } - (NSArray *)menuItemsImages { if (// Not logged in) { _menuItemsImages = @[@"foo.png", @"bar.png", @"baz.png"]; return _menuItemsImages; } else { .... return _menuItemsImages; } } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.menuItems.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MenuCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } NSString *menuItem = self.menuItems[indexPath.row]; cell.textLabel.text = menuItem; cell.imageView.image = [UIImage imageNamed:self.menuItemsImages[indexPath.row]]; [cell setBackgroundColor:[UIColor clearColor]]; return cell; }

pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *menuItem = self.menuItems[indexPath.row]; if ([menuItem isEqualToString:@"foo"]) { self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"fooNavigationVC"]; } else if ([menuItem isEqualToString:@"bar"]) { self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"barNavigationController"]; } else if ([menuItem isEqualToString:@"baz"]) { self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"bazNavigationViewController"]; } [self.slidingViewController resetTopViewAnimated:YES]; }

I'm a little confused because with that method the tableview sometimes does not update.

As far as I understand it right now I see two solutions: - Check is user is logged in ViewWillAppear and change the content - Set the Menu in ViewDidLoad and send a notification to the MenuViewController if user logs in, and [self.tableView reloadData];

It also have that checking which cell is pressed by comparing string is not very good.

If anyone could suggest some better ideas about how to change that menu's content depending on the user's logged ing or not would be great. thx

最满意答案

据我所知,我现在看到两个解决方案: - 检查用户是否登录ViewWillAppear并更改内容 - 在ViewDidLoad中设置菜单,如果用户登录则向MenuViewController发送通知,[self.tableView reloadData] ;

你不是在做这个吗? 如果希望更改表视图的内容,则必须在该表视图上调用reloadData 。 我知道没有其他方法可以做到这一点。 我不知道你是如何处理登录/退出逻辑的,所以我不能告诉你这里最好的东西,但我猜一个通知可能是一个很好的起点。


你是对的 - 比较字符串以确定按下哪一行,因此加载哪个视图是不好的做法。

didSelectRowAtIndexPath:已经发送了一个参数,告诉我们选择了哪个单元格: indexPath 。

字符串比较存在许多问题。 首先,如果您想要本地化您的应用程序,由于所有不同的语言,字符串将根据使用该应用程序的人而有所不同。 其次,字符串比较相对于仅仅比较构成索引路径的几个整数而言是缓慢的。 第三,有时您需要稍微改变实际的字符串。 它可能像拼写错误一样简单。 您输入该字符串的位置越多,您输入拼写错误的可能性就越大,您需要花费更多时间对每个位置进行双/三次检查以确保每个字符串完全相同 - 直到大小写。

鉴于您似乎只有一个部分但只有多行,这里最简单的解决方案可能是一个简单的enum 。

typedef NS_ENUM(NSInteger, MenuIndexPath) { RowForFoo = 0, RowForBar = 1, RowForBaz = 2, RowForQuux = 0, RowForOther = 1, RowForAnother = 2 };

现在我们将在cellForRowAtIndexPath:和didSelectRowAtIndexPath:使用此枚举。

switch(indexPath.row) { case RowForFoo: cell.textLabel.title = @"foo"; break; // etc... }

...

switch(indexPath.row) { case RowForFoo: self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"fooNavigationVC"]; break; // etc... }

现在,如果我们需要改变“foo”行中单元格的外观,我们只会去调整单元格外观的方法。 如果我们需要改变关于“foo”行的单元格如何响应被选中的任何内容,我们只会转到该方法。 如果我们需要更改行“foo”出现的顺序,我们可以修改enum 。

如果您使用多个部分,只需为部分创建enum ,并为行创建enum 。

As far as I understand it right now I see two solutions: - Check is user is logged in ViewWillAppear and change the content - Set the Menu in ViewDidLoad and send a notification to the MenuViewController if user logs in, and [self.tableView reloadData];

Are you not doing any of this? If you want the contents of a table view to change, you must call reloadData on that table view. There's no other way that I know of to do it. I don't know how you're handling the log in / log out logic, so I can't tell you what would be best here, but I'd guess a notification is probably a good starting place.


You are correct--comparing strings to determine which row was pressed and therefore which view to load is bad practice.

didSelectRowAtIndexPath: is already sending an argument that tells us which cell was selected: the indexPath.

There are many problems with string comparison. First of all, if you ever want to localize your app, the strings will be different depending on who is using the app because of all the different languages. Second of all, string comparison is slow relative to just comparing a couple ints that make up an index path. And third, sometimes you need to slightly change what the actual string is. It could be something as simple as a typo. The more places you have to type that string at, the more likely it is you make the typo, and the more time you have to spend double/triple checking every location to make sure every string is exactly the same--down to the capitalization.

Given that you appear to have just a single section but multiple rows, the simplest solution here would probably be a simple enum.

typedef NS_ENUM(NSInteger, MenuIndexPath) { RowForFoo = 0, RowForBar = 1, RowForBaz = 2, RowForQuux = 0, RowForOther = 1, RowForAnother = 2 };

Now we'll use this enum both in cellForRowAtIndexPath: and didSelectRowAtIndexPath:.

switch(indexPath.row) { case RowForFoo: cell.textLabel.title = @"foo"; break; // etc... }

...

switch(indexPath.row) { case RowForFoo: self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"fooNavigationVC"]; break; // etc... }

Now, if we need to change anything about how the cell at row "foo" looks, we only go to the the methods that adjust how cells look. If we need to change anything about how the cell at row "foo" responds to being selected, we only go to that method. And if we need to change the order in which row "foo" appears, we can go modify the enum.

If you're using multiple sections, just make an enum for the sections and an enum for the rows.

更多推荐

self,tableView,cell,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-07-05 01:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容   用户   UITableView   change   User

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!