Ios UITableView内部UIView-仅在UITableView外部滚动单元格时加载数据
我在Ios UITableView内部UIView-仅在UITableView外部滚动单元格时加载数据,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在UIViewController内使用UITableView时遇到问题: 启动应用程序时,会显示UITableView,但不会显示来自对手的数据。 当我开始滚动UITableView之外的第一个单元格时,现在该单元格将被更新。 此外,当我手动触发[\u TopComponents reloaddata]时,整个UITableView显示正确的数据 我必须添加或更改什么,以便从一开始就显示数据 dataSource和delegate都与故事板中的类连接 这是我的代码: viewDidLoad:
UIViewController
内使用UITableView
时遇到问题:
启动应用程序时,会显示UITableView
,但不会显示来自对手的数据。
当我开始滚动UITableView
之外的第一个单元格时,现在该单元格将被更新。
此外,当我手动触发[\u TopComponents reloaddata]时,整个UITableView
显示正确的数据
我必须添加或更改什么,以便从一开始就显示数据
dataSource
和delegate
都与故事板中的类连接
这是我的代码:viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
myappdel = (AppDelegate *)[UIApplication sharedApplication].delegate;
self.title = @"New Match";
_lopponent.text = _opponent;
opponents = [[NSMutableArray alloc] init];
NSMutableDictionary *opponent = [[NSMutableDictionary alloc] init];
[opponent setObject:@"opponent A" forKey:@"name"];
[opponent setObject:@"xx matches | xx wins || xx losts" forKey:@"stats"];
[opponent setObject:@"Krems" forKey:@"location"];
NSMutableDictionary *opponent2 = [[NSMutableDictionary alloc] init];
[opponent2 setObject:@"opponent B" forKey:@"name"];
[opponent2 setObject:@"yy matches | yy wins || yy losts" forKey:@"stats"];
[opponent2 setObject:@"Location B" forKey:@"location"];
NSMutableDictionary *opponent3 = [[NSMutableDictionary alloc] init];
[opponent3 setObject:@"opponent C" forKey:@"name"];
[opponent3 setObject:@"zz matches | zz wins || zz losts" forKey:@"stats"];
[opponent3 setObject:@"Location C" forKey:@"location"];
[opponents addObject:opponent];
[opponents addObject:opponent2];
[opponents addObject:opponent3];
[_topponents reloadData];
}
这里是我的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"opponentcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lopponentname = (UILabel*)[cell viewWithTag:1];
UILabel *lopponentstats = (UILabel*)[cell viewWithTag:2];
UILabel *llocation = (UILabel*)[cell viewWithTag:3];
lopponentname.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"name"];
lopponentstats.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"stats"];
llocation.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"location"];
return cell;
}
我想,问题出在牢房里。创建UITableViewCell子类
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"opponentcell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.lopponentname.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.lopponentstats.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"stats"];
cell.llocation.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"location"];
return cell;
}
希望对您有所帮助。在单元格初始化后添加此项。对我来说,工作正常
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
当单元格为零时,您需要检查条件。。 最初,单元格为零。。 所以数据只有在滚动后才会加载。。。 初始化单元格后,在cellForRowAtIndexPath委托方法中添加以下代码行
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
希望这能解决问题…我认为您使用的是自定义cel,而且看起来您在“CellForRowatineXpath”中遇到了问题。如果您使用.xib,您可能会发现它很有用
static NSString *cellIdentifier = @"opponentcell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibObjects =[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:nil options:nil];
for (id currentObject in nibObjects)
{
if ([currentObject isKindOfClass:[CustomTableViewCell class]])
{
cell = (CustomTableViewCell *) currentObject;
}
}
[cell initViewStyles];
}
我发现了我的错
我曾经写道:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
但是我应该用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
与“forIndexPath”的区别在于,前者可能返回
nil
,而后者将在适当的地方创建一个对象。后者是一种更现代、更值得推荐的方式,但所有提倡手动检查的人都给了你一个可行的解决方案。