2 NSArrays 在一个 UITableView 中,标签作为子视图?
我当前的应用程序有问题。它在 UIViewController
中有一个 UITableView
。我在底部有一个 UIButton(在 UITableView 之外)。它以这种方式工作:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"bla"]) {
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"bla"];
[tableView reloadData];
} else {
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"tasks2do"];
[tableView reloadData]; }
当我以这种方式使用 cell.textLabel.text
方法时,这是有效的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ident = @"indet";
cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];
}
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"bla"]) {
cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
}
return cell; }
现在我想使用 UILabel
而不是 cell。 textLabel
,因为我出于某些原因需要它(例如,设置标签框架)
为此,我使用了以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ident = @"indet";
cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];
}
UILabel *thislabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 44)] autorelease];
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"bla"]) {
[thislabel setText:[firstArray objectAtIndex:indexPath.row]];
} else {
[thislabel setText:[secondArray objectAtIndex:indexPath.row]];
}
[cell.contentView addSubview:thislabel];
return cell; }
效果很好,直到我按下 UIButton
进行切换。它会切换,单元格显示新文本,但新文本后面仍然是旧文本,如下所示:
(第一个数组包含字母 L,第二个数组包含字母 J,它混合了两者)
自从我尝试了一些东西(例如使用 2 < code>UILabel 的数组并隐藏一个)?会很酷的。 :)
我希望我的英语不是太难理解,我的英语写作能力不是最好的,对此我很抱歉。
如果您需要更多信息/代码,只需发布它,应该不成问题。
I'm having a problem with my current App. It has one UITableView
in the UIViewController
. I have one UIButton
at the bottom (out of the UITableView
). It works in that way:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"bla"]) {
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"bla"];
[tableView reloadData];
} else {
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"tasks2do"];
[tableView reloadData]; }
This worked when I had the cell.textLabel.text
Method in this way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ident = @"indet";
cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];
}
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"bla"]) {
cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
}
return cell; }
Now I want to use an UILabel
instead of cell.textLabel
, because I need it for some reasons (eg. setting the labels frame)
For that I used the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ident = @"indet";
cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];
}
UILabel *thislabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 44)] autorelease];
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"bla"]) {
[thislabel setText:[firstArray objectAtIndex:indexPath.row]];
} else {
[thislabel setText:[secondArray objectAtIndex:indexPath.row]];
}
[cell.contentView addSubview:thislabel];
return cell; }
That works fine, until I push the UIButton
for switching. It switches, the cell shows the new text but behind the new text is still the old text as you can see here:
(the firstArray contains the letter L, the secondArray contains the Letter J, it mixes up both up)
Do you have any idea for solving this problem since I tried some stuff (for example using 2 UILabel
s for the arrays and hide one)? Would be cool. :)
I hope my English is not too bad to understand, my English skills for writing aren't the best, I'm sorry for that.
If you need further information / code just post it, shouldn't be a problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您创建一个 UITableViewCell 子类,在其中配置标签(设置框架并将其添加为 UITableViewCell 初始化程序中的子视图)。添加一个属性来设置标签中的文本,并为该属性编写一个像这样的设置器:
编辑: 顺便说一句,您要处理的问题是缓存。每次单元格出现在视图中时,您都会重新创建一个新标签,即使该单元格之前已经有一个标签。发生这种情况是因为您在 UITableViewCell 初始值设定项之外初始化了 UILabel(只应为每个缓存单元调用一次,之后可以从缓存中检索它,包括其所有子视图)。
I recommend you create a UITableViewCell subclass in which you configure the label (set frame and add it as subview in UITableViewCell's initializer). Add a property for setting the text in the label and write a setter like this for the property:
Edit: by the way, the issue you're dealing with is the caching. You recreate a new label every time a cell comes in view, even if the cell already had a label before. This happens because you initialize an UILabel outside the UITableViewCell initializer (which only should be called once for every cached cell, afterwards it can be retrieved from cache, including all it's subviews).