我是 ios 世界的新手,我有一些数据要加载到 TableView Controller 中,并且加载完美。现在,如果完成了新插入,则新数据将从索引 0 开始添加到 NSMutableArray 中(新数据是在顶部,旧数据在它们之后)现在用新数据更新 NSMutableArray 后,我称之为 [tableview reloadData]。
我的目标是在 TableView Controller 的顶部显示新数据(索引 0),但没有加载任何新数据!为什么?
这是我用来加载表格单元格的代码,其中数据是包含我的对象的 NSMutableArray :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
// add description
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(15,25, 260, 50)];
text.numberOfLines=3;
text.text= [[data objectAtIndex:indexPath.row] desc];
text.backgroundColor=[UIColor clearColor];
text.textAlignment= UITextAlignmentRight;
text.font = [UIFont fontWithName:@"Helvetica" size:13];
[cell addSubview:text];
}
//use your cell here
cell.textLabel.textAlignment=UITextAlignmentRight;
cell.textLabel.numberOfLines =3;
// cell font
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0];
cell.textLabel.textColor=[UIColor blackColor];
return cell;
}
最佳答案
如果您查看仅在第一次创建单元格时将值分配给 UILabel 的代码,则在任何单元格!= nil 的出队方法中,您只需更改已存在的标签的字体和颜色自您使用 StyleValue1 以来的单元格
// add description
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(15,25, 260, 50)];
text.numberOfLines=3;
text.text= [[data objectAtIndex:indexPath.row] desc];
text.backgroundColor=[UIColor clearColor];
text.textAlignment= UITextAlignmentRight;
text.font = [UIFont fontWithName:@"Helvetica" size:13];
[cell addSubview:text];
如果您只使用已经与单元格关联的标签,则不需要此属性,否则,如果您确实需要此属性,则应提供标签标记,以便在显示时可以从单元格中再次检索它.
还将 subview 添加到单元格而不是单元格的内容 View 。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{ UILabel *文本; NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
// add description
text = [[UILabel alloc] initWithFrame:CGRectMake(15,25, 260, 50)];
text.numberOfLines=3;
text.backgroundColor=[UIColor clearColor];
text.tag = 1000;
text.textAlignment= UITextAlignmentRight;
text.font = [UIFont fontWithName:@"Helvetica" size:13];
[cell addSubview:text];
} else {
text = (UILabel*)[cell viewForTag:1000];
}
// Here you update the value of text
text.text= [[data objectAtIndex:indexPath.row] desc];
//use your cell here
cell.textLabel.textAlignment=UITextAlignmentRight;
cell.textLabel.numberOfLines =3;
// cell font
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0];
cell.textLabel.textColor=[UIColor blackColor];
return cell;
}
关于iphone - 在 uitableviewcontroller 顶部添加新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18836499/