UITableView一遍又一遍地重复细胞

时间:2015-11-04 23:24:05

标签: ios objective-c uitableview

所以我有这个UITableView,它一遍又一遍地重复单元格,我不明白为什么,我已经尝试使用谷歌搜索并查看其他类似的堆栈溢出问题,但其中没有一个解决方案似乎解决了问题我有。

我的代码如下:

ViewDidLoad方法中的NSArray:

tableViewContents = @[
    @"Infosecurity Netherlands:4th November:Jaarbeurs Utrecht-Utrecht,Netherlands:www.infosecurity.nl/",
    @"MiCS Monaco International Clubbing Show:4th - 6th November:Grimaldi Forum-Monaco, Monaco:www.mics.mc/",
    @"New York Audio Show 2015:6th - 8th November:Hilton Westchester-Rye Brook, NY:www.chestergroup.org/newyorkaudioshow/2015",
    @"Tecno Multimedia InfoComm 2015:10th - 12th November:Corferias-Bogota, Columbia:www.tecnomultimedia.com/",
    @"Productronica 2015:10th - 13th November:New Munich Trade Show Center-Munich, Germany:productronica.com/en/home",
    @"CCW featuring SATCON co-located InfoComm Connections:11th November:Jacob Javits Center, New York, NY:www.ccwexpo.com/",
    @"IAAPA Attractions Expo 2015:16th - 20th November:Orange County Convention Center-Orlando, FL:www.iaapa.org/expos/iaapa-attractions-expo/",
    @"Streaming Media West 2015:17th November:Hyatt Regency Huntington Beach-Huntington Beach, CA:www.streamingmedia.com/Conferences/",
    @"SATIS:17th - 19th November:Porte de Versailles-Paris, France:www.satis-expo.com/",
    @"ISC East Expo 2015:18th November:Jacob K. Javits Convention Center-New York, NY:www.isceast.com/",
    @"WFX Worship Facilities Conference and Expo:18th November:Music City Center-Nashville, TN:wfxweb.com/",
    @"Inter BEE 2015:18th - 20th November:Makuhari Messe-Chiba, Japan:www.inter-bee.com/en/",
    @"AV Executive Conference:18th - 20th November:TBA-Amelia Island, FL:www.infocomm.org/cps/rde/xchg/infocomm/hs.xsl/39221.htm",
    @"JTSE Performance & Entertainment Technical Show & Convention 2015:24th November:Dock Pullman, Porte de la Chapelle-Paris, France:www.jtse.fr/en/index.php",
    @"Government Video Expo 2015:1st - 3rd December:Walter E. Washington Convention Center-Washington, DC:www.gvexpo.com/",
    @"IIDEX Canada:2nd December:Metro Toronto Convention Centre North-Toronto, ON, Canada:www.iidexcanada.com/",
    @"IABM Annual International Business Conference 2015:3rd December:TBA:www.theiabm.org/",
    @"SMSS The Social Media Strategies Summit Dallas:8th December:The Magnolia Hotel-Dallas, TX:www.socialmediastrategiessummit.com/dallas-2015/",
    @"CineAsia:8th - 10th December:Hong Kong Convention & Exhibition Centre-Hong Kong, China:www.vnufilmgroup.com/cineasia",
    @"CeBIT Bilisim Eurasia:17th - 19th December:Istanbul Expo Center-Istanbul, Turkey:www.cebitbilisim.com/en/index.php"
    ];

表格查看方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

//Amount of sections
return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

//Amount of rows in each section
return [tableViewContents count];

}

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


NSString *contentsString = tableViewContents[indexPath.row];
NSArray *contentsArray = [contentsString componentsSeparatedByString:@":"];

//Cell Identifier
static NSString *simpleIdentifier = @"simpleIdentifier";

UILabel *eventNameLabel;
UILabel *eventDateLabel;
UILabel *eventLocationLabel;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];

    eventNameLabel = [[UILabel alloc]init];
    eventDateLabel = [[UILabel alloc]init];
    eventLocationLabel = [[UILabel alloc]init];


    eventNameLabel.frame = CGRectMake(10, 10, 300, 20);
    eventNameLabel.font = [UIFont boldSystemFontOfSize:14];
    eventNameLabel.textColor = [UIColor colorWithRed:231.0f/255.0f
                                               green:123.0f/255.0f
                                                blue:50.0f/255.0f
                                               alpha:1.0f];


    eventDateLabel.frame = CGRectMake(10, 40, 300, 20);
    eventDateLabel.font = [UIFont systemFontOfSize:12];
    eventDateLabel.textColor = [UIColor grayColor];

    eventLocationLabel.frame = CGRectMake(10, 70, 300, 20);
    eventLocationLabel.font = [UIFont systemFontOfSize:12];
    eventLocationLabel.textColor = [UIColor grayColor];

    [cell addSubview:eventNameLabel];
    [cell addSubview:eventDateLabel];
    [cell addSubview:eventLocationLabel];

}

eventNameLabel.text = contentsArray[0];
eventDateLabel.text = contentsArray[1];
eventLocationLabel.text = contentsArray[2];


//Return Cell
return cell;

}

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

Start of the UITableView

Here the UITableView has stared to repeat the first couple of cells

提前感谢您的帮助。

4 个答案:

答案 0 :(得分:2)

对于重复使用的单元格,

eventNameLabel和其他两个标签为nil,因此您永远不会根据需要更新标签。

您需要在else添加if语句,将三个标签变量设置为现有单元格中的标签。

当然,更好的方法是使用具有三个标签的三个属性的自定义单元类。不要将所有逻辑放在视图控制器中。它属于细胞类。

答案 1 :(得分:1)

您的代码无法按照书面形式运行。想一想。您的cellForRowAtIndexPath方法有3个局部变量eventNameLabel,eventDateLabel和eventLocationLabel。如果您创建一个新单元格,那么这些变量将指向您的标签。

如果您回收现有单元格,那么这些变量将为零。

您需要另一种方式来获取标签。如何在创建单元格时为其分配标记,然后在处理回收单元格的代码中使用这些标记查找标签。

编辑:

或者,正如rmaddy在他的答案中建议用1分钟击败我的,使用自定义单元格类,其中单元格的IBOutlet属性链接到标签。

答案 2 :(得分:1)

您可以尝试在界面中添加3个标签,这样您就可以访问单元格中的标签了。把它放进去

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

-

NSArray *subviews = [[cell contentView] subviews];
UILabel *eventNameLabel = [subviews objectAtIndex:0];
UILabel *eventDateLabel  = [subviews objectAtIndex:1];
UILabel *eventLocationLabel = [subviews objectAtIndex:2];

有了这个,您也不必添加子视图。另一种方法是你可以创建一个UITableViewCell类,你可以链接到单元格,然后像在视图控制器中一样控制拖动标签到单元类文件。

答案 3 :(得分:1)

您的要求的明确答案是Custom Cell。而不是在cellforrowaatIndexpath方法中进行所有编码,根据您的要求自定义单元格并将其用于表格。处理自定义单元格会容易得多。请参阅这些链接以了解如何自定义tableViewCell。自定义单元格http://www.appcoda.com/customize-table-view-cells-for-uitableview/http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/