简体   繁体   English

截面的UITableView

[英]sectioned UITableView

I am making an application for the iPhone, in which I am creating UITableView with 2 sections. 我正在为iPhone制作一个应用程序,在其中创建包含2个部分的UITableView。 I have created sections parts of UITableView, but the problem is that I need different images in every cell of each section. 我已经创建了UITableView的各个部分,但是问题是我在每个部分的每个单元格中需要不同的图像。 Does anyone has any solution for this 有谁对此有任何解决方案

Yes, that's very easy. 是的,这很容易。 Have you this in your datasource yet:? 您在数据源中是否有此功能?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Add the following code to it: 向其添加以下代码:

NSString *CellIdentifier = [NSString stringWithFormat:@"cell_%i",indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    if (indexPath.section == 0 && indexPath.row == 0){
        [cell.imageView setImage:[UIImage imageNamed:@"justanimage.png"]];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        [cell.imageView setImage:[UIImage imageNamed:@"justanimage1.png"]];
    }else if (indexPath.section == 1 && indexPath.row == 0){
        [cell.imageView setImage:[UIImage imageNamed:@"justanimage2.png"]];
    }else if (indexPath.section == 1 && indexPath.row == 1){
        [cell.imageView setImage:[UIImage imageNamed:@"justanimage3.png"]];
    }
}

You can also save your data in a NSMutableArray, but that's a little bit more complicated. 您也可以将数据保存在NSMutableArray中,但这有点复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM