UITableView的嵌套使用


  本文原創,轉載請注明出處!!  

  緊趕慢趕,拼了命的去學習工作,還是沒有完成......傷心啊,自己的技術是在太差了,要學的東西太多,現在要做一個大一點的項目就顯得力不從心了。

  先把工作扔一邊吧,雖然自己學的東西不多,不過還是把自己覺得有用的東西分享出來給大家看,讓大家能夠更快的學習,有時間多去學習學習,就不會像小白豬這樣提筆忘詞了。

  UITableView是cocoa框架中使用的非常多的一個控件,相比其他控件還是有點復雜的,下面分享自己做的一個UITableView的嵌套的例子,效果是一個縱向的table,每一個節點里面又有一個橫向的table,可以橫向縱向滑動,類似土豆網iphone版的視頻列表。

效果圖如下:

 

 

其實原理非常簡單,先定義一個普通的UITableView,設置它的行數和節點數。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;//每個節點只有一行
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;//7個節點
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;//設置每一個節點的高
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section]==0) {
        NSLog(@"indexPath section%d",[indexPath section]);
        NSLog(@"rows === %d",[indexPath row]);//在這里可以添加點擊觸發執行的方法
    }
}

下面要注意了,是重點,作用是將每一個節點里都添加一個UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellname = @"Cell";
    InfoCell *cell = (InfoCell *)[tableView dequeueReusableCellWithIdentifier:cellname];//InfoCell是一個類,在里面重新定義了橫向的UITableView,下面有介紹
    if (cell == nil){
    cell = [[[InfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellname number:0]autorelease];
        }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

下面是InfoCell這個類的.m文件里的代碼,不是很難,重點我會說明。

下面的方法中我只添加了_dataArray1,其實還有_dataArray2 ,_dataArray3,為了節省空間代碼省去了。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        tableSection = [passValue passValue_];
 //       porsection = tableSection.num;
        hortable = [[UITableView alloc]initWithFrame:CGRectMake(100, -90, 140, 320) style:UITableViewStylePlain];
        hortable.delegate = self;
        hortable.dataSource = self;
        [self addSubview:hortable];
        hortable.transform = CGAffineTransformMakeRotation(M_PI / 2 *3);//這里是將table旋轉270度,如果是90度的話,相當於整個橫向的UITableView也反了。
        NSString  *str = nil;
        _dataArray1 = [[NSMutableArray alloc] initWithCapacity:0];
        str = @"小白豬是頭大笨豬!!!!";
        [_dataArray1 addObject:str];
        [_dataArray1 addObject:str];
        [_dataArray1 addObject:str];//在數組里添加了一段字符串,在table中顯示,各位可以在這里給每一行添加不同的字符串。不過需要做個判斷
    }
    return self;
}

接下來是設置具體的每一行,這里笨豬沒有想到好的方法,因為cell會重用,我傳了一個參數porsection來橫向的table在縱向的table中的是在第幾個節點的位置。如果有高手的話,請給出更好的方法,感激不盡。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *CellIdentifier1 = @"cell1";//每一行定義取不同的名字來取消重用。
    static NSString *CellIdentifier2 = @"cell2";
    static NSString *CellIdentifier3 = @"cell3";
    static NSString *CellIdentifier4 = @"cell4";
    static NSString *CellIdentifier5 = @"cell5";
    static NSString *CellIdentifier6 = @"cell6";
    static NSString *CellIdentifier7 = @"cell7";
    
        switch (porsection) {
            case 0:
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
                cell.transform = CGAffineTransformMakeRotation(M_PI/2); //將cell中的文字旋轉90度
                [[cell textLabel] setText:[_dataArray1 objectAtIndex:indexPath.row]];
                cell.textLabel.lineBreakMode = UILineBreakModeClip;
                cell.textLabel.numberOfLines = 0;

                break;
            case 1:
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
                cell.transform = CGAffineTransformMakeRotation(M_PI/2);
                [[cell textLabel] setText:[_dataArray2 objectAtIndex:indexPath.row]];
                cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
                cell.textLabel.numberOfLines = 0;
                break;
                
            case 2:
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3]autorelease];
                cell.transform = CGAffineTransformMakeRotation(M_PI/2);
                [[cell textLabel] setText:[_dataArray3 objectAtIndex:indexPath.row]];
                break;

            case 3:
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier4]autorelease];
                cell.transform = CGAffineTransformMakeRotation(M_PI/2);
                [[cell textLabel] setText:[_dataArray4 objectAtIndex:indexPath.row]];
                break;

            case 4:
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier5]autorelease];
                cell.transform = CGAffineTransformMakeRotation(M_PI/2);
                [[cell textLabel] setText:[_dataArray5 objectAtIndex:indexPath.row]];
                break;

            case 5:
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier6]autorelease];
                cell.transform = CGAffineTransformMakeRotation(M_PI/2);

                [[cell textLabel] setText:[_dataArray6 objectAtIndex:indexPath.row]];
                cell.textLabel.lineBreakMode = UILineBreakModeClip;
                break;
            case 6:
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier7]autorelease];
                cell.transform = CGAffineTransformMakeRotation(M_PI/2);

                [[cell textLabel] setText:[_dataArray7 objectAtIndex:indexPath.row]];
                break;
            default:
                break;
        }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

還有一個困擾我的問題,我給每一行都添加不同的數據,為什么從第5行開始就重復第一行的內容,第6行重復第2行的,依此類推,我總是想不通,望高手指明。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM