如何TableView中
问题描述:
添加不同的自定义单元格我有多个自定义单元格。我将其中的两个子类化。我想用故事板显示具有不同标识符的不同单元格。到目前为止,使用一个数组我能够显示一个子类的单元格,但是我的行数和显示多个单元格的技术有问题。我使用延迟来不断添加和更新我的表。如何TableView中
NSMutableArray *conv1;
NSString *l1;
NSString *l2;
NSString *l3;
NSMutableArray *allDialogue;
NSMutableArray *conv2;
}
@end
@implementation PlotController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
conv1 = [[NSMutableArray alloc] init];
conv2 = [[NSMutableArray alloc] init];
allDialogue = [[NSMutableArray alloc] init];
[allDialogue addObjectsFromArray:conv1];
[allDialogue addObjectsFromArray:conv2];
l1 = @"converstaion1";
l2 = @"converstaion2";
l3 = @"converstaion3";
_tableV.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[self performSelector:@selector(delay) withObject:nil afterDelay:2.0];
[self performSelector:@selector(delay2) withObject:nil afterDelay:4.0];
}
实现代码如下配置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MainStoryDialogue *dialogueCell = [tableView dequeueReusableCellWithIdentifier:@"PDialogue"];
dialogueCell.textHere.text = [allDialogue objectAtIndex:indexPath.row];
[self.tableV beginUpdates];
[self.tableV endUpdates];
return dialogueCell;
}
第二个问题是行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [allDialogue count];
}
答
我认为,能在表视图来显示一个以上的自定义单元格,第一,在你的故事板中,你需要点击你的tableView,在属性检查器下面你会看到一个名为Prototype Cells的字段。选择您想要使用的不同单元的数量,根据需要自定义它们,为每个单元创建一个TableViewCell类,进行所有适当的连接,并为每个单元设置一个唯一标识。已经这样做了,在你的cellForRowAtIndexPath方法(无论基于何种条件下),你可以实例,你想为使用您创建的自定义单元格类及相应的小区标识给定行特定的细胞。
因此,例如:
if (indexpath.row == 1) {
MainStoryDialogue *dialogueCell = [tableViewdequeueReusableCellWithIdentifier:@"PDialogue"];
//do all appropriate things if you have this cell
} else {
CustomCell2 *customCell2 = [tableView dequeueReusableCellWithIdentifier:@"customCell2"];
//do all appropriate things if you had this cell
}
加入多个定制的细胞就像我说 – Legenetic
顺便说一句,在开始和结束的更新呼叫您'cellForRowAtIndexPath'表视图控制器生命周期方法什么都不做。 – andrewbuilder