[英]Adding static content into UITableViewCell
我使用的是UIViewController而不是UITableViewcontroller,并且无法理解将静态内容添加到表中的概念。 我以前以编程方式完成了动态内容,但从未完成过静态内容。
这是我在IB中设计的:
所有这些内容都位于TableCell的“内容视图”内部。
我已经设置了数据源和委托,我只有一个部分并返回了一个单元格。 这是我的单元格代码。 通常,我会使用数组来填充它,但是我不知道如何使用IB中的静态内容来填充它。 如何添加图片中的对象,最重要的是如何保留每个对象的位置? 我希望我不必手动输入x,y坐标。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
return cell;
}
编辑:静态单元格在UITableViewController中可以正常工作。
您必须将UITableViewController用于静态单元格。
这是我的解决方法usign UIViewController。
- (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
NSString * titol = [NSString new];
NSString * valor = [NSString new];
NSDictionary * el_contacte = [contactes objectAtIndex:(indexPath.row)];
switch (indexPath.row) {
case 0:
titol = @"Nombre";
valor = [el_contacte valueForKey:@"nombre_contacto"];
break;
case 1:
titol = @"Teléfono";
valor = [el_contacte valueForKey:@"telefono"];
break;
case 2:
titol = @"Extensión";
valor = [el_contacte valueForKey:@"extension"];
break;
case 3:
titol = @"Móvil";
valor = [el_contacte valueForKey:@"movil"];
break;
case 4:
titol = @"Fax";
valor = [el_contacte valueForKey:@"fax"];
break;
case 5:
titol = @"email";
valor = [el_contacte valueForKey:@"email"];
break;
default:
break;
}
cell.textLabel.text = titol;
cell.detailTextLabel.text = valor;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString * retornar = [NSString new];
switch (section) {
case 0:
retornar = @"Datos del cliente";
break;
case 1:
retornar = @"Contacto";
break;
case 3:
retornar = @"Personas";
break;
default:
break;
}
return retornar;
}
是的,您可以在UIViewController中完成此操作,但是您仍将以静态方式使用动态单元格。 就是说,我的意思是,您在情节提要中设置了单元格的所有子视图,将所需的任何文本放置在标签中,将图像放置在任何图像视图中,等等。然后在cellForRowAtIndexPath中,您只需使单元格出队并返回它,而不执行其他任何操作。 如果您有多个具有不同内容的单元格,则需要创建不同的动态原型,每个原型都有自己的标识符。 在cellForRowAtIndexPath中,使用if-else块测试传入的indexPathRow,并在该位置出队所需的正确单元格类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.