【发布时间】:2014-05-13 07:16:21
【问题描述】:
我有一个超出屏幕区域的UITableView。我显示了来自NSMutableArray 的一些数据,其中填充了NSDictionary 对象。所有对象都具有group 属性,即true 或false,如果group 为真,则应在单元格中显示UIButton(以展开组)。
我在storyboard 的单元格prototype 中创建了一个UIButton。我给了它alpha 0。如果我加载列表,则按钮不在任何单元格中(duh)。这是我的cellForRowAtIndexPath:
if ([[contacts[indexPath.section] valueForKey:@"group"] isEqualToString:@"true"]) {
UIButton *button = (UIButton*)[cell viewWithTag:999];
[button setAlpha:1.0f];
}
如果group 属性为true,则使按钮可见。在我当前的测试用例中,前三个对象的group 设置为true,其余的group 设置为false。现在我的问题是,前三个单元格有一个可见按钮(如预期的那样),但表格中更远的一些单元格也有一个。我在上面的if statement中放了一个断点,它真的只进去了3次。
它以一种模式发生,前三个单元格有一个按钮,然后五个单元格没有,然后另外三个单元格有一个可见按钮。比另外五个没有按钮,最后一个单元格有(我想如果我添加两个单元格,它们也会有一个按钮)
第二组有按钮的单元格就在屏幕外开始,所以这可能与它有关。
更相关的事情,还有一些显示的文本是我从与组标志相同的数组中获取的,该文本显示正确。所以并不是细胞以某种方式被复制,只是if statement 部分搞砸了。
我仔细检查了程序进入if statement 的次数,实际上只有三次(前三个条目)。如果我评论按钮上的setAlpha 调用,任何地方都不会出现按钮,因此也没有其他地方可以看到按钮。
我在装有 iOS 7.1 的 iPad 4 上进行测试。
如果我降低行的高度以使所有行都适合屏幕,那么问题就消失了,所以这肯定与加载数据时行落在屏幕区域之外有关。
编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
UIColor *color = [UIColor colorWithRed:(1.0f/255.0f)*220 green:(1.0f/255.0f)*220 blue:(1.0f/255.0f)*225 alpha:1.0f];
if (indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"single" forIndexPath:indexPath];
UILabel *screenname = (UILabel*)[cell viewWithTag:10];
UILabel *username = (UILabel*)[cell viewWithTag:11];
UIImageView *avatar = (UIImageView*)[cell viewWithTag:20];
if ([[contacts[indexPath.section] valueForKey:@"group"] isEqualToString:@"true"]) {
NSDictionary *c = contacts[indexPath.section];
UIButton *button = (UIButton*)[cell viewWithTag:999];
[button setAlpha:1.0f];
button.tag = indexPath.section;
[button addTarget:self action:@selector(onGroupButtonClick:) forControlEvents:UIControlEventTouchUpInside];
int numUsersInGroup = [[c valueForKey:@"users"] count];
NSString *numUsersInGroupString = [NSString stringWithFormat:@"%d",numUsersInGroup];
[button setTitle:numUsersInGroupString forState:0];
[button setNeedsDisplay];
screenname.text = @"Groep";
username.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"groupName"]];
} else {
User *c = (User*)contacts[indexPath.section];
UIButton *button = (UIButton*)[cell viewWithTag:999];
[button setAlpha:0.0f];
screenname.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"screenName"]];
username.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"username"]];
[avatar setImage:[UIImage imageNamed:@"avatar.png"]];
}
[GeneralFunctions setFontFamily:@"KozGoPro-ExtraLight" forView:cell andSubViews:YES];
[GeneralFunctions setFontFamily:@"KozGoPro-Medium" forView:screenname andSubViews:YES];
[cell.layer setShadowColor:[color CGColor]];
[cell.layer setShadowOffset:CGSizeMake(0,4)];
[cell.layer setShadowRadius:3.0f];
[cell.layer setShadowOpacity:1.0f];
cell.clipsToBounds = NO;
cell.layer.masksToBounds = NO;
}
if (indexPath.row > 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"group" forIndexPath:indexPath];
UILabel *screenname = (UILabel*)[cell viewWithTag:10];
UILabel *username = (UILabel*)[cell viewWithTag:11];
User *c = (User*)[contacts[indexPath.section] valueForKey:@"users"][indexPath.row-1];
screenname.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"screenName"]];
username.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"username"]];
[GeneralFunctions setFontFamily:@"KozGoPro-ExtraLight" forView:cell andSubViews:YES];
[GeneralFunctions setFontFamily:@"KozGoPro-Medium" forView:screenname andSubViews:YES];
}
return cell;
}
【问题讨论】:
标签: ios iphone objective-c ipad uitableview