Ios 不在动态UITableView中插入值
我正在尝试创建一个动态的应用程序,它使用API自动更新。问题是它确实将正确的部分与正确的装置连接起来,并且装置似乎在不同的部分中重复。为什么不将节日期与装置连接起来 首先,我有一个fixture数组,其中包含所有fixtureIos 不在动态UITableView中插入值,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试创建一个动态的应用程序,它使用API自动更新。问题是它确实将正确的部分与正确的装置连接起来,并且装置似乎在不同的部分中重复。为什么不将节日期与装置连接起来 首先,我有一个fixture数组,其中包含所有fixture [fixtures addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:date,@"date", time, @"time", competition_id,@"competition_id", h
[fixtures addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:date,@"date", time, @"time", competition_id,@"competition_id", home, @"home", away, @"away", nil]];
然后我有一个数组,包含所有日期,没有重复项,按日期排序,称为ReverseArray:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject: descriptor];
NSArray *reverseOrder = [dateArray sortedArrayUsingDescriptors:descriptors];
reversedArray = [[reverseOrder reverseObjectEnumerator] allObjects];
然后我就有了自己的观点
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 22)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width, 22)];
[label setFont:[UIFont boldSystemFontOfSize:14]];
[label setTextColor:[UIColor whiteColor]];
NSString *dateString = [NSString stringWithFormat:@"%@",[reversedArray objectAtIndex:section]];
NSString *newString = [dateString substringToIndex:[dateString length]-15];
NSString *string =[NSString stringWithFormat:@"%@",newString];
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:13/255.0f green:78/255.0f blue:102/255.0f alpha:1.0f]];
return view;
}
截面法中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *dateString = [NSString stringWithFormat:@"%@",[reversedArray objectAtIndex:section]];
NSLog(@"%@ = %@", [dateString substringToIndex:[dateString length]-15], [[fixtures objectAtIndex:section] valueForKey:@"date"]);
NSArray *filteredArray = [fixtures filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"date == %@",[dateString substringToIndex:[dateString length]-15]]];
return filteredArray.count;
}
cellForRowAtIndexPath方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"away" ];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"home"];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"away"];
return cell;
}
从你的if语句中去掉这个代码
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"away" ];
您正在重用单元格,因此条件cell=nil将始终不为真
还可以使用indexPath.section for了解填充单元格时应获取哪些数据更改此方法cellForRowAtIndexPath方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"away" ];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"home"];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"away"];
return cell;
}
如果(cell==nil)重用单元标识符在单元创建后未进入循环,则在外部分配标签值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"away" ];
return cell;
}
如果您使用的是自定义单元格,请尝试以下操作
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"FixtureCell";
FixtureCell *cell = (FixtureCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FixtureCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"home"];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:@"away"];
return cell;
}
如果(cell==nil){cell=[[FixtureCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:@“cell”];}
(括号末尾),则cell.homeTeamLabel.text
,等等。并不是说您的代码似乎没有返回单元格
。发送到实例的未识别选择器这给了我发送到实例的未识别选择器。为什么在每个单元格文本中indexpath.row两次?r u使用自定义表单元格??