我有 2 个简单的课程:
Class1 {
int i;
NSString s;
}
Class2 {
NSString name;
NSString age;
}
在我的 UIViewController 中,我有 2 个 NSMutableArray,每个数组包含多个 Class1 和 Class2 对象。
我需要为每个数组创建一个新部分,并且部分中的行数应该等于该数组上的对象数。同样在每个单元格中,我需要显示每个对象变量的数据。
请帮忙!!!
我有 2 个简单的课程:
Class1 {
int i;
NSString s;
}
Class2 {
NSString name;
NSString age;
}
在我的 UIViewController 中,我有 2 个 NSMutableArray,每个数组包含多个 Class1 和 Class2 对象。
我需要为每个数组创建一个新部分,并且部分中的行数应该等于该数组上的对象数。同样在每个单元格中,我需要显示每个对象变量的数据。
请帮忙!!!
试试下面的代码...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return self.class1Array.count;
case 1:
return self.class2Array.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell==nil)
{
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
switch(indexPath.section)
{
case 0:
cell.textLabel.text=[self.class1Array objectAtIndex:indexPath.row] s];
cell.detailTextLabel.text=[self.class1Array objectAtIndex:indexPath.row] i];
return cell;
case 1:
cell.textLabel.text=[self.class2Array objectAtIndex:indexPath.row] name];
cell.detailTextLabel.text=[self.class1Array objectAtIndex:indexPath.row] age];
return cell;
}
}
编辑:
如果您想创建自定义单元格,请尝试这样做
尝试
if(tableview.section==0)
{
cell.label.text= [aray1 object at index :indexpath.row]
}
else if(tableview.section==1)
{
cell.label.text= [aray object at index :indexpath.row]
}